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.

233 lines
6.6KB

  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. const nframes_t end = start + len;
  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. cnt = _clip->read( buf + ofs, channel, start, end );
  96. /* apply gain */
  97. buffer_apply_gain( buf + ofs, cnt, _scale );
  98. /* perform declicking if necessary */
  99. /* FIXME: keep the declick defults someplace else */
  100. Fade declick;
  101. declick.length = 256;
  102. declick.type = Fade::Linear;
  103. {
  104. Fade fade;
  105. fade = declick < _fade_in ? _fade_in : declick;
  106. /* do fade in if necessary */
  107. if ( sofs < fade.length )
  108. {
  109. const long d = 0 - sofs;
  110. assert( cnt <= nframes );
  111. fade.apply( buf + ofs, Fade::In, d, d + fade.length, cnt );
  112. }
  113. fade = declick < _fade_out ? _fade_out : declick;
  114. /* do fade out if necessary */
  115. // if ( start + cnt + fade.length > r.end )
  116. if ( start + fade.length > ( r.offset + r.length ) )
  117. {
  118. const nframes_t d = ( r.offset + r.length ) - start;
  119. assert( cnt <= nframes );
  120. fade.apply( buf, Fade::Out, cnt + (long)d - fade.length, cnt + d, cnt );
  121. }
  122. }
  123. // printf( "read %lu frames\n", cnt );
  124. return cnt;
  125. }
  126. /** prepare for capturing */
  127. void
  128. Audio_Region::prepare ( void )
  129. {
  130. log_start();
  131. }
  132. /* THREAD: IO */
  133. /** write /nframes/ from /buf/ to source. /buf/ is interleaved and
  134. must match the channel layout of the write source! */
  135. nframes_t
  136. Audio_Region::write ( nframes_t nframes )
  137. {
  138. _range.length += nframes;
  139. /* FIXME: too much? */
  140. // _track->damage( FL_DAMAGE_EXPOSE, x() + w(), y(), 10/* FIXME: guess */, h() );
  141. if ( 0 == ( timeline->ts_to_x( _range.length ) % 20 ) )
  142. {
  143. nframes_t oldl = _clip->length();
  144. /* get the new size. Remember, this is a read-only handle on the source--not the same
  145. one being written to */
  146. _clip->close();
  147. _clip->open();
  148. int W = timeline->ts_to_x( _clip->length() - oldl );
  149. /* why - 1? */
  150. if ( W )
  151. {
  152. ++W;
  153. Fl::lock();
  154. sequence()->damage( FL_DAMAGE_ALL, x() + w() - W, y(), W, h() );
  155. Fl::awake();
  156. Fl::unlock();
  157. }
  158. }
  159. return nframes;
  160. }
  161. /* THREAD: IO */
  162. /** finalize region capture. Assumes that this *is* a captured region
  163. and that no other regions refer to the same source */
  164. bool
  165. Audio_Region::finalize ( nframes_t frame )
  166. {
  167. _range.length = frame - _range.start;
  168. log_end();
  169. _clip->close();
  170. _clip->open();
  171. /* FIXME: should we attempt to truncate the file? */
  172. Fl::lock();
  173. redraw();
  174. Fl::awake();
  175. Fl::unlock();
  176. return true;
  177. }