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.

256 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. /** Apply a (portion of) fade from /start/ to /end/ assuming a
  25. * buffer size of /nframes/. /start/ and /end/ are relative to the
  26. * given buffer, and /start/ may be negative. */
  27. void
  28. Audio_Region::Fade::apply ( sample_t *buf, Audio_Region::Fade::fade_dir_e dir, long start, nframes_t end, nframes_t nframes ) const
  29. {
  30. // printf( "apply fade %s: start=%ld end=%lu\n", dir == Fade::Out ? "out" : "in", start, end );
  31. if ( ! nframes )
  32. return;
  33. const nframes_t i = start > 0 ? start : 0;
  34. const nframes_t e = end > nframes ? nframes : end;
  35. assert( i < nframes );
  36. const float inc = increment();
  37. float fi = ( i - start ) / (float)length;
  38. // buf += i;
  39. buf = &buf[ i ];
  40. nframes_t n = e - i;
  41. assert( i + n <= nframes );
  42. if ( dir == Fade::Out )
  43. {
  44. fi = 1.0f - fi;
  45. for ( ; n--; fi -= inc )
  46. *(buf++) *= gain( fi );
  47. }
  48. else
  49. for ( ; n--; fi += inc )
  50. *(buf++) *= gain( fi );
  51. }
  52. /* THREAD: IO */
  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. const Range r = _range;
  68. /* do nothing if we aren't covered by this frame range */
  69. if ( pos > r.start + r.length || pos + nframes < r.start )
  70. return 0;
  71. /* calculate offsets into file and sample buffer */
  72. nframes_t sofs, ofs, cnt;
  73. cnt = nframes;
  74. if ( pos < r.start )
  75. {
  76. sofs = 0;
  77. ofs = r.start - pos;
  78. cnt -= ofs;
  79. }
  80. else
  81. {
  82. ofs = 0;
  83. sofs = pos - r.start;
  84. }
  85. if ( ofs >= nframes )
  86. return 0;
  87. // const nframes_t start = ofs + r.start + sofs;
  88. const nframes_t start = r.offset + sofs;
  89. const nframes_t len = min( cnt, nframes - ofs );
  90. if ( len == 0 )
  91. return 0;
  92. /* now that we know how much and where to read, get on with it */
  93. // printf( "reading region ofs = %lu, sofs = %lu, %lu-%lu\n", ofs, sofs, start, end );
  94. if ( _loop )
  95. {
  96. nframes_t lofs = sofs % _loop;
  97. nframes_t lstart = r.offset + lofs;
  98. if ( lofs + len > _loop )
  99. {
  100. /* this buffer covers a loop bounary */
  101. /* read the first part */
  102. cnt = _clip->read( buf + ofs, channel, lstart, len - ( ( lofs + len ) - _loop ) );
  103. /* read the second part */
  104. cnt += _clip->read( buf + ofs + cnt, channel, lstart + cnt, len - cnt );
  105. /* TODO: declick/crossfade transition? */
  106. assert( cnt == len );
  107. }
  108. else
  109. cnt = _clip->read( buf + ofs, channel, lstart, len );
  110. }
  111. else
  112. cnt = _clip->read( buf + ofs, channel, start, len );
  113. /* apply gain */
  114. buffer_apply_gain( buf + ofs, cnt, _scale );
  115. /* perform declicking if necessary */
  116. /* FIXME: keep the declick defults someplace else */
  117. Fade declick;
  118. declick.length = 256;
  119. declick.type = Fade::Linear;
  120. {
  121. Fade fade;
  122. fade = declick < _fade_in ? _fade_in : declick;
  123. /* do fade in if necessary */
  124. if ( sofs < fade.length )
  125. {
  126. const long d = 0 - sofs;
  127. assert( cnt <= nframes );
  128. fade.apply( buf + ofs, Fade::In, d, d + fade.length, cnt );
  129. }
  130. fade = declick < _fade_out ? _fade_out : declick;
  131. /* do fade out if necessary */
  132. // if ( start + cnt + fade.length > r.end )
  133. if ( start + fade.length > ( r.offset + r.length ) )
  134. {
  135. const nframes_t d = ( r.offset + r.length ) - start;
  136. assert( cnt <= nframes );
  137. fade.apply( buf, Fade::Out, cnt + (long)d - fade.length, cnt + d, cnt );
  138. }
  139. }
  140. // printf( "read %lu frames\n", cnt );
  141. return cnt;
  142. }
  143. /** prepare for capturing */
  144. void
  145. Audio_Region::prepare ( void )
  146. {
  147. log_start();
  148. }
  149. /* THREAD: IO */
  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. _range.length += nframes;
  156. /* FIXME: too much? */
  157. // _track->damage( FL_DAMAGE_EXPOSE, x() + w(), y(), 10/* FIXME: guess */, h() );
  158. if ( 0 == ( timeline->ts_to_x( _range.length ) % 20 ) )
  159. {
  160. nframes_t oldl = _clip->length();
  161. /* get the new size. Remember, this is a read-only handle on the source--not the same
  162. one being written to */
  163. _clip->close();
  164. _clip->open();
  165. int W = timeline->ts_to_x( _clip->length() - oldl );
  166. /* why - 1? */
  167. if ( W )
  168. {
  169. ++W;
  170. Fl::lock();
  171. sequence()->damage( FL_DAMAGE_ALL, x() + w() - W, y(), W, h() );
  172. // Fl::awake();
  173. Fl::unlock();
  174. }
  175. }
  176. return nframes;
  177. }
  178. /* THREAD: IO */
  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. DMESSAGE( "finalizing capture region" );
  185. _range.length = frame - _range.start;
  186. log_end();
  187. _clip->close();
  188. _clip->open();
  189. /* FIXME: should we attempt to truncate the file? */
  190. Fl::lock();
  191. redraw();
  192. Fl::awake();
  193. Fl::unlock();
  194. return true;
  195. }