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
7.0KB

  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 ( 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, _channel, 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. const char *
  143. Peaks::peakname ( const char *filename ) const
  144. {
  145. static char file[512];
  146. snprintf( file, 512, "%s.peak-%d", filename, _channel );
  147. return (const char*)&file;
  148. }
  149. bool
  150. Peaks::open ( void )
  151. {
  152. const char *filename = _clip->name();
  153. int fd;
  154. make_peaks( 256 );
  155. if ( ( fd = ::open( peakname( filename ), O_RDONLY ) ) < 0 )
  156. return false;
  157. {
  158. struct stat st;
  159. fstat( fd, &st );
  160. _len = st.st_size;
  161. }
  162. _peaks = (peakdata*)mmap( NULL, _len, PROT_READ, MAP_SHARED, fd, 0 );
  163. ::close( fd );
  164. if ( _peaks == MAP_FAILED )
  165. printf( "failed to create mapping!\n" );
  166. _len = (_len - sizeof( int )) / sizeof( Peak );
  167. return true;
  168. }
  169. /** returns false if peak file for /filename/ is out of date */
  170. bool
  171. Peaks::current ( void ) const
  172. {
  173. int sfd, pfd;
  174. if ( ( sfd = ::open( _clip->name(), O_RDONLY ) ) < 0 )
  175. return true;
  176. if ( ( pfd = ::open( peakname( _clip->name() ), O_RDONLY ) ) < 0 )
  177. return false;
  178. struct stat sst, pst;
  179. fstat( sfd, &sst );
  180. fstat( pfd, &pst );
  181. close( sfd );
  182. close( pfd );
  183. return sst.st_mtime <= pst.st_mtime;
  184. }
  185. /** build peaks file for /filename/ if necessary */
  186. bool
  187. Peaks::make_peaks ( int chunksize )
  188. {
  189. const char *filename = _clip->name();
  190. if ( current() )
  191. return true;
  192. if ( ! _clip->open() )
  193. return false;
  194. FILE *fp = fopen( peakname( filename ), "w" );
  195. if ( fp == NULL )
  196. {
  197. _clip->close();
  198. return false;
  199. }
  200. /* write chunksize first */
  201. fwrite( &chunksize, sizeof( int ), 1, fp );
  202. size_t len;
  203. do {
  204. Peak p;
  205. len = clip_read_peaks( &p, 1, chunksize );
  206. fwrite( &p, sizeof( Peak ), 1, fp );
  207. }
  208. while ( len );
  209. _clip->close();
  210. fclose( fp );
  211. return true;
  212. }
  213. /** return normalization factor for range of samples from /start/ to
  214. /end/ (uses known peak data if possible */
  215. float
  216. Peaks::normalization_factor( nframes_t start, nframes_t end ) const
  217. {
  218. float s;
  219. fill_buffer( start, end );
  220. Peak p = peak( start, end );
  221. s = fabs( 1.0f / p.max );
  222. if ( s * p.min < -1.0 )
  223. s = 1 / fabs( p.max );
  224. return s;
  225. }