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.

313 lines
7.2KB

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