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.

329 lines
7.5KB

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