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.

306 lines
6.9KB

  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 "Clip.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 ( int s, int e ) const
  37. {
  38. if ( timeline.fpp < _peaks->chunksize )
  39. {
  40. /* looks like we're going to have to switch to a higher resolution peak file
  41. or read directly from the source */
  42. read_peaks( s, e, (e - s) / timeline.fpp, timeline.fpp );
  43. }
  44. else
  45. {
  46. /* we'll just downsample on the fly in this case--no need for extra copying into
  47. the buffer */
  48. }
  49. }
  50. void
  51. Peaks::downsample ( Peak *peaks, int s, int e, float *mhi, float *mlo ) const
  52. {
  53. *mhi = 0;
  54. *mlo = 0;
  55. if ( e > _len )
  56. e = _len;
  57. for ( int j = s; j < e; j++ )
  58. {
  59. const float lo = peaks[ j ].min;
  60. const float hi = peaks[ j ].max;
  61. if ( hi > *mhi )
  62. *mhi = hi;
  63. if ( lo < *mlo )
  64. *mlo = lo;
  65. }
  66. }
  67. int
  68. Peaks::clip_read_peaks ( Peak *peaks, int npeaks, int chunksize ) const
  69. {
  70. sample_t *fbuf = new sample_t[ chunksize ];
  71. size_t len;
  72. int i;
  73. for ( i = 0; i < npeaks; ++i )
  74. {
  75. /* read in a buffer */
  76. len = _clip->read( fbuf, chunksize );
  77. Peak &p = peaks[ i ];
  78. p.min = 0;
  79. p.max = 0;
  80. for ( int j = len; j--; )
  81. {
  82. if ( fbuf[j] > p.max )
  83. p.max = fbuf[j];
  84. if ( fbuf[j] < p.min )
  85. p.min = fbuf[j];
  86. }
  87. if ( len < chunksize )
  88. break;
  89. }
  90. delete fbuf;
  91. return i;
  92. }
  93. void
  94. Peaks::read_peaks ( int s, int e, int npeaks, int chunksize ) const
  95. {
  96. printf( "reading peaks %d @ %d\n", npeaks, chunksize );
  97. if ( peakbuf.size < npeaks )
  98. {
  99. peakbuf.size = npeaks;
  100. // printf( "reallocating peak buffer %li\n", peakbuf.size );
  101. peakbuf.buf = (peakdata*)realloc( peakbuf.buf, sizeof( peakdata ) + (peakbuf.size * sizeof( Peak )) );
  102. }
  103. _clip->open();
  104. _clip->seek( s );
  105. peakbuf.offset = s;
  106. peakbuf.buf->chunksize = chunksize;
  107. peakbuf.len = clip_read_peaks( peakbuf.buf->data, npeaks, chunksize );
  108. _clip->close();
  109. }
  110. /** Return the peak for the range of samples */
  111. Peak &
  112. Peaks::peak ( nframes_t start, nframes_t end ) const
  113. {
  114. /* Is there a better way to return this? */
  115. static Peak p;
  116. if ( timeline.fpp < _peaks->chunksize )
  117. {
  118. assert( timeline.fpp == peakbuf.buf->chunksize );
  119. start = (start - peakbuf.offset) / peakbuf.buf->chunksize;
  120. end = (end - peakbuf.offset) / peakbuf.buf->chunksize;
  121. if ( end > peakbuf.len )
  122. end = peakbuf.len;
  123. // assert( peakbuf.len > start );
  124. downsample( peakbuf.buf->data, start, end, &p.max, &p.min );
  125. }
  126. else
  127. {
  128. start /= _peaks->chunksize;
  129. end /= _peaks->chunksize;
  130. downsample( _peaks->data, start, end, &p.max, &p.min );
  131. }
  132. return p;
  133. }
  134. /* virtual array. Index is a Pixel value, and it returns the
  135. * (resampled) peaks for that pixel based on the current timeline
  136. * zoom. */
  137. Peak &
  138. Peaks::operator[] ( int X ) const
  139. {
  140. return peak( timeline.x_to_ts( X ), timeline.x_to_ts( X + 1 ) );
  141. }
  142. static
  143. const char *
  144. peakname ( const char *filename )
  145. {
  146. static char file[512];
  147. snprintf( file, 512, "%s.peak", filename );
  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( nframes_t start, nframes_t end ) const
  218. {
  219. float s;
  220. fill_buffer( start, end );
  221. Peak p = peak( start, end );
  222. s = fabs( 1.0f / p.max );
  223. if ( s * p.min < -1.0 )
  224. s = 1 / fabs( p.max );
  225. return s;
  226. }