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.

311 lines
7.1KB

  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. static
  67. int
  68. sf_read_peaks ( SNDFILE *in, Peak *peaks, int npeaks, int chunksize )
  69. {
  70. float *fbuf = new float[ chunksize ];
  71. size_t len;
  72. int i;
  73. for ( i = 0; i < npeaks; ++i )
  74. {
  75. /* read in a buffer */
  76. len = sf_read_float( in, 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. memset( peakbuf.buf->data, 0, peakbuf.size * sizeof( Peak ) );
  104. SNDFILE *in;
  105. SF_INFO si;
  106. memset( &si, 0, sizeof( si ) );
  107. in = sf_open( _clip->name(), SFM_READ, &si );
  108. sf_seek( in, s, SEEK_SET );
  109. peakbuf.offset = s;
  110. peakbuf.buf->chunksize = chunksize;
  111. peakbuf.len = sf_read_peaks( in, peakbuf.buf->data, npeaks, chunksize );
  112. sf_close( in );
  113. }
  114. /* virtual array. Index is a Pixel value, and it returns the
  115. * (resampled) peaks for that pixel based on the current timeline
  116. * zoom. */
  117. Peak &
  118. Peaks::operator[] ( int X ) const
  119. {
  120. /* Is there a better way to return this? */
  121. static Peak p;
  122. if ( timeline.fpp < _peaks->chunksize )
  123. {
  124. assert( timeline.fpp == peakbuf.buf->chunksize );
  125. int start = timeline.x_to_ts( X ) / peakbuf.buf->chunksize;
  126. int i = start - (peakbuf.offset / peakbuf.buf->chunksize);
  127. assert( peakbuf.len > i );
  128. p = peakbuf.buf->data[ i ];
  129. }
  130. else
  131. {
  132. int start = timeline.x_to_ts( X ) / _peaks->chunksize;
  133. int end = timeline.x_to_ts( X + 1 ) / _peaks->chunksize;
  134. downsample( start, end, &p.max, &p.min );
  135. }
  136. return p;
  137. }
  138. static
  139. const char *
  140. peakname ( const char *filename )
  141. {
  142. static char file[512];
  143. snprintf( file, 512, "%s.peak", filename );
  144. return (const char*)&file;
  145. }
  146. bool
  147. Peaks::open ( const char *filename )
  148. {
  149. int fd;
  150. make_peaks( filename, 256 );
  151. if ( ( fd = ::open( peakname( filename ), O_RDONLY ) ) < 0 )
  152. return false;
  153. {
  154. struct stat st;
  155. fstat( fd, &st );
  156. _len = st.st_size;
  157. }
  158. _peaks = (peakdata*)mmap( NULL, _len, PROT_READ, MAP_SHARED, fd, 0 );
  159. ::close( fd );
  160. if ( _peaks == MAP_FAILED )
  161. printf( "failed to create mapping!\n" );
  162. _len = (_len - sizeof( int )) / sizeof( Peak );
  163. return true;
  164. }
  165. void
  166. long_to_float( long *buf, int len )
  167. {
  168. for ( int i = len; i--; )
  169. *((float*)buf) = *buf / 32768;
  170. }
  171. /** returns false if peak file for /filename/ is out of date */
  172. bool
  173. Peaks::current ( const char *filename )
  174. {
  175. int sfd, pfd;
  176. if ( ( sfd = ::open( filename, O_RDONLY ) ) < 0 )
  177. return true;
  178. if ( ( pfd = ::open( peakname( filename ), O_RDONLY ) ) < 0 )
  179. return false;
  180. struct stat sst, pst;
  181. fstat( sfd, &sst );
  182. fstat( pfd, &pst );
  183. close( sfd );
  184. close( pfd );
  185. return sst.st_mtime <= pst.st_mtime;
  186. }
  187. /** build peaks file for /filename/ if necessary */
  188. bool
  189. Peaks::make_peaks ( const char *filename, int chunksize )
  190. {
  191. if ( current( filename ) )
  192. return true;
  193. SNDFILE *in;
  194. SF_INFO si;
  195. memset( &si, 0, sizeof( si ) );
  196. in = sf_open( filename, SFM_READ, &si );
  197. if ( si.channels != 1 )
  198. abort();
  199. if ( si.samplerate != timeline.sample_rate )
  200. abort();
  201. FILE *fp = fopen( peakname( filename ), "w" );
  202. if ( fp == NULL )
  203. {
  204. sf_close( in );
  205. /* return fals */
  206. return false;
  207. }
  208. /* write chunksize first */
  209. fwrite( &chunksize, sizeof( int ), 1, fp );
  210. float *fbuf = new float[ chunksize ];
  211. size_t len;
  212. do {
  213. /* read in a buffer */
  214. len = sf_read_float( in, fbuf, chunksize );
  215. Peak p;
  216. p.max = -1.0;
  217. p.min = 1.0;
  218. for ( int i = 0; i < len; ++i )
  219. {
  220. if ( fbuf[i] > p.max )
  221. p.max = fbuf[i];
  222. if ( fbuf[i] < p.min )
  223. p.min = fbuf[i];
  224. }
  225. fwrite( &p, sizeof( Peak ), 1, fp );
  226. }
  227. while ( len == chunksize );
  228. fclose( fp );
  229. sf_close( in );
  230. delete fbuf;
  231. }