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.

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