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.

327 lines
7.4KB

  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. #include "Peaks.H"
  19. // #include "Timeline.H"
  20. #include <sys/mman.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <sndfile.h>
  29. #include "Audio_File.H"
  30. #include "assert.h"
  31. #include <math.h>
  32. Peaks::peakbuffer Peaks::_peakbuf;
  33. /** Prepare a buffer of peaks from /s/ to /e/ for reading. Must be
  34. * called before any calls to operator[] */
  35. int
  36. Peaks::fill_buffer ( float fpp, int s, int e ) const
  37. {
  38. _fpp = fpp;
  39. /* FIXME: repair this */
  40. // if ( fpp < _peaks->chunksize )
  41. {
  42. /* looks like we're going to have to switch to a higher resolution peak file
  43. or read directly from the source */
  44. read_peaks( s, e, (e - s) / fpp, fpp );
  45. /* FIXME: are we *SURE* we got them all? */
  46. return e - s;
  47. }
  48. /* else */
  49. /* { */
  50. /* /\* we'll just downsample on the fly in this case--no need for extra copying into */
  51. /* the buffer *\/ */
  52. /* } */
  53. }
  54. void
  55. Peaks::downsample ( Peak *peaks, int s, int e, float *mhi, float *mlo ) const
  56. {
  57. *mhi = 0;
  58. *mlo = 0;
  59. if ( e > _len )
  60. e = _len;
  61. for ( int j = s; j < e; j++ )
  62. {
  63. const float lo = peaks[ j ].min;
  64. const float hi = peaks[ j ].max;
  65. if ( hi > *mhi )
  66. *mhi = hi;
  67. if ( lo < *mlo )
  68. *mlo = lo;
  69. }
  70. }
  71. int
  72. Peaks::clip_read_peaks ( Peak *peaks, int npeaks, int chunksize ) const
  73. {
  74. sample_t *fbuf = new sample_t[ chunksize ];
  75. size_t len;
  76. int i;
  77. for ( i = 0; i < npeaks; ++i )
  78. {
  79. /* read in a buffer */
  80. len = _clip->read( fbuf, _channel, chunksize );
  81. Peak &p = peaks[ i ];
  82. p.min = 0;
  83. p.max = 0;
  84. for ( int j = len; j--; )
  85. {
  86. if ( fbuf[j] > p.max )
  87. p.max = fbuf[j];
  88. if ( fbuf[j] < p.min )
  89. p.min = fbuf[j];
  90. }
  91. if ( len < chunksize )
  92. break;
  93. }
  94. delete fbuf;
  95. return i;
  96. }
  97. void
  98. Peaks::read_peaks ( int s, int e, int npeaks, int chunksize ) const
  99. {
  100. printf( "reading peaks %d @ %d\n", npeaks, chunksize );
  101. if ( _peakbuf.size < npeaks )
  102. {
  103. _peakbuf.size = npeaks;
  104. // printf( "reallocating peak buffer %li\n", _peakbuf.size );
  105. _peakbuf.buf = (peakdata*)realloc( _peakbuf.buf, sizeof( peakdata ) + (_peakbuf.size * sizeof( Peak )) );
  106. }
  107. _clip->open();
  108. _clip->seek( s );
  109. _peakbuf.offset = s;
  110. _peakbuf.buf->chunksize = chunksize;
  111. _peakbuf.len = clip_read_peaks( _peakbuf.buf->data, npeaks, chunksize );
  112. _clip->close();
  113. }
  114. /** Return the peak for the range of samples */
  115. Peak &
  116. Peaks::peak ( nframes_t start, nframes_t end ) const
  117. {
  118. /* Is there a better way to return this? */
  119. static Peak p;
  120. if ( _fpp < _peaks->chunksize )
  121. {
  122. assert( _fpp == _peakbuf.buf->chunksize );
  123. start = (start - _peakbuf.offset) / _peakbuf.buf->chunksize;
  124. end = (end - _peakbuf.offset) / _peakbuf.buf->chunksize;
  125. if ( end > _peakbuf.len )
  126. end = _peakbuf.len;
  127. // assert( _peakbuf.len > start );
  128. downsample( _peakbuf.buf->data, start, end, &p.max, &p.min );
  129. }
  130. else
  131. {
  132. start /= _peaks->chunksize;
  133. end /= _peaks->chunksize;
  134. downsample( _peaks->data, start, end, &p.max, &p.min );
  135. }
  136. return p;
  137. }
  138. /* virtual array. Index is a Pixel value, and it returns the
  139. * (resampled) peaks for that pixel based on the current timeline
  140. * zoom. */
  141. /* Peak & */
  142. /* Peaks::operator[] ( int X ) const */
  143. /* { */
  144. /* Peak p; */
  145. /* p.min = 0; */
  146. /* p.max = 0; */
  147. /* return p; */
  148. /* // return peak( timeline->x_to_ts( X ), timeline->x_to_ts( X + 1 ) ); */
  149. /* } */
  150. const char *
  151. Peaks::peakname ( const char *filename ) const
  152. {
  153. static char file[512];
  154. snprintf( file, 512, "%s.peak-%d", filename, _channel );
  155. return (const char*)&file;
  156. }
  157. bool
  158. Peaks::open ( void )
  159. {
  160. const char *filename = _clip->name();
  161. int fd;
  162. make_peaks( 256 );
  163. if ( ( fd = ::open( peakname( filename ), O_RDONLY ) ) < 0 )
  164. return false;
  165. {
  166. struct stat st;
  167. fstat( fd, &st );
  168. _len = st.st_size;
  169. }
  170. _peaks = (peakdata*)mmap( NULL, _len, PROT_READ, MAP_SHARED, fd, 0 );
  171. ::close( fd );
  172. if ( _peaks == MAP_FAILED )
  173. printf( "failed to create mapping!\n" );
  174. _len = (_len - sizeof( int )) / sizeof( Peak );
  175. return true;
  176. }
  177. /** returns false if peak file for /filename/ is out of date */
  178. bool
  179. Peaks::current ( void ) const
  180. {
  181. int sfd, pfd;
  182. if ( ( sfd = ::open( _clip->name(), O_RDONLY ) ) < 0 )
  183. return true;
  184. if ( ( pfd = ::open( peakname( _clip->name() ), O_RDONLY ) ) < 0 )
  185. return false;
  186. struct stat sst, pst;
  187. fstat( sfd, &sst );
  188. fstat( pfd, &pst );
  189. close( sfd );
  190. close( pfd );
  191. return sst.st_mtime <= pst.st_mtime;
  192. }
  193. /** build peaks file for /filename/ if necessary */
  194. bool
  195. Peaks::make_peaks ( int chunksize )
  196. {
  197. const char *filename = _clip->name();
  198. if ( current() )
  199. return true;
  200. if ( ! _clip->open() )
  201. return false;
  202. FILE *fp = fopen( peakname( filename ), "w" );
  203. if ( fp == NULL )
  204. {
  205. _clip->close();
  206. return false;
  207. }
  208. /* write chunksize first */
  209. fwrite( &chunksize, sizeof( int ), 1, fp );
  210. size_t len;
  211. do {
  212. Peak p;
  213. len = clip_read_peaks( &p, 1, chunksize );
  214. fwrite( &p, sizeof( Peak ), 1, fp );
  215. }
  216. while ( len );
  217. _clip->close();
  218. fclose( fp );
  219. return true;
  220. }
  221. /** return normalization factor for range of samples from /start/ to
  222. /end/ (uses known peak data if possible */
  223. float
  224. Peaks::normalization_factor( float fpp, nframes_t start, nframes_t end ) const
  225. {
  226. float s;
  227. // fill_buffer( fpp, start, end );
  228. if ( end - start < _peaks->chunksize * 4 )
  229. fill_buffer( _clip->length() / 4, start, end );
  230. else
  231. fill_buffer( _clip->length(), start, end );
  232. Peak p = peak( start, end );
  233. s = 1.0f / fabs( p.max );
  234. if ( s * p.min < -1.0 )
  235. s = 1.0f / fabs( p.min );
  236. return s;
  237. }