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.

320 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. void
  31. Peaks::downsample ( int s, int e, float *mhi, float *mlo ) const
  32. {
  33. *mhi = -1.0;
  34. *mlo = 1.0;
  35. if ( e > _len )
  36. e = _len;
  37. for ( int j = s; j < e; j++ )
  38. {
  39. const float lo = _peaks->data[ j ].min;
  40. const float hi = _peaks->data[ j ].max;
  41. if ( hi > *mhi )
  42. *mhi = hi;
  43. if ( lo < *mlo )
  44. *mlo = lo;
  45. }
  46. }
  47. void
  48. Peaks::read ( int X, float *hi, float *lo ) const
  49. {
  50. int start = X * timeline.fpp;
  51. int end = (X + 1) * timeline.fpp;
  52. downsample( start, end, hi, lo );
  53. }
  54. static
  55. int
  56. sf_read_peaks ( SNDFILE *in, Peak *peaks, int npeaks, int chunksize )
  57. {
  58. float *fbuf = new float[ chunksize ];
  59. size_t len;
  60. int i;
  61. for ( i = 0; i < npeaks; ++i )
  62. {
  63. /* read in a buffer */
  64. len = sf_read_float( in, fbuf, chunksize );
  65. float hi = -1.0;
  66. float lo = 1.0;
  67. for ( int j = len; j--; )
  68. {
  69. if ( fbuf[j] > hi )
  70. hi = fbuf[j];
  71. if ( fbuf[j] < lo )
  72. lo = fbuf[j];
  73. }
  74. peaks[ i ].max = hi;
  75. peaks[ i ].min = lo;
  76. if ( len < chunksize )
  77. break;
  78. }
  79. delete fbuf;
  80. return i;
  81. }
  82. void
  83. Peaks::read_peaks ( int s, int e, float *mhi, float *mlo ) const
  84. {
  85. static Peak * peaks_read = NULL;
  86. static nframes_t peaks_read_offset = 0;
  87. const int buffer_size = BUFSIZ;
  88. if ( ! peaks_read )
  89. peaks_read = new Peak[ buffer_size ];
  90. else
  91. {
  92. if ( s >= peaks_read_offset &&
  93. e - peaks_read_offset < buffer_size )
  94. goto done;
  95. if ( e > peaks_read_offset + buffer_size )
  96. {
  97. printf( "hit buffer boundardy!\n" );
  98. memmove( peaks_read, &peaks_read[ (s - peaks_read_offset) ], (buffer_size - (s - peaks_read_offset)) * sizeof( Peak ) );
  99. peaks_read_offset = s;
  100. goto done;
  101. }
  102. }
  103. /* this could be faster, but who cares. Don't zoom in so far! */
  104. {
  105. SNDFILE *in;
  106. SF_INFO si;
  107. memset( &si, 0, sizeof( si ) );
  108. in = sf_open( _clip->name(), SFM_READ, &si );
  109. sf_seek( in, s, SEEK_SET );
  110. peaks_read_offset = s;
  111. int chunksize = e - s;
  112. printf( "read %d peaks\n", sf_read_peaks( in, peaks_read, buffer_size, chunksize ) );
  113. sf_close( in );
  114. }
  115. done:
  116. // FIXME: should downsample here?
  117. Peak p = peaks_read[ s - peaks_read_offset ];
  118. *mhi = p.max;
  119. *mlo = p.min;
  120. }
  121. /* virtual array. Index is a Pixel value, and it returns the
  122. * (resampled) peaks for that pixel based on the current timeline
  123. * zoom. */
  124. Peak &
  125. Peaks::operator[] ( int X ) const
  126. {
  127. /* Is there a better way to return this? */
  128. static Peak p;
  129. if ( timeline.fpp < _peaks->chunksize )
  130. {
  131. int start = timeline.x_to_ts( X );
  132. int end = timeline.x_to_ts( X + 1 );
  133. read_peaks( start, end, &p.max, &p.min );
  134. }
  135. else
  136. {
  137. int start = timeline.x_to_ts( X ) / _peaks->chunksize;
  138. int end = timeline.x_to_ts( X + 1 ) / _peaks->chunksize;
  139. downsample( start, end, &p.max, &p.min );
  140. }
  141. return p;
  142. }
  143. static
  144. const char *
  145. peakname ( const char *filename )
  146. {
  147. static char file[512];
  148. snprintf( file, 512, "%s.peak", filename );
  149. return (const char*)&file;
  150. }
  151. bool
  152. Peaks::open ( const char *filename )
  153. {
  154. int fd;
  155. make_peaks( filename, 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 = (peaks*)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. void
  171. long_to_float( long *buf, int len )
  172. {
  173. for ( int i = len; i--; )
  174. *((float*)buf) = *buf / 32768;
  175. }
  176. /** returns false if peak file for /filename/ is out of date */
  177. bool
  178. Peaks::current ( const char *filename )
  179. {
  180. int sfd, pfd;
  181. if ( ( sfd = ::open( filename, O_RDONLY ) ) < 0 )
  182. return true;
  183. if ( ( pfd = ::open( peakname( filename ), O_RDONLY ) ) < 0 )
  184. return false;
  185. struct stat sst, pst;
  186. fstat( sfd, &sst );
  187. fstat( pfd, &pst );
  188. close( sfd );
  189. close( pfd );
  190. return sst.st_mtime <= pst.st_mtime;
  191. }
  192. /** build peaks file for /filename/ if necessary */
  193. bool
  194. Peaks::make_peaks ( const char *filename, int chunksize )
  195. {
  196. if ( current( filename ) )
  197. return true;
  198. SNDFILE *in;
  199. SF_INFO si;
  200. memset( &si, 0, sizeof( si ) );
  201. in = sf_open( filename, SFM_READ, &si );
  202. if ( si.channels != 1 )
  203. abort();
  204. if ( si.samplerate != timeline.sample_rate )
  205. abort();
  206. FILE *fp = fopen( peakname( filename ), "w" );
  207. if ( fp == NULL )
  208. {
  209. sf_close( in );
  210. /* return fals */
  211. return false;
  212. }
  213. /* write chunksize first */
  214. fwrite( &chunksize, sizeof( int ), 1, fp );
  215. float *fbuf = new float[ chunksize ];
  216. size_t len;
  217. do {
  218. /* read in a buffer */
  219. len = sf_read_float( in, fbuf, chunksize );
  220. Peak p;
  221. p.max = -1.0;
  222. p.min = 1.0;
  223. for ( int i = 0; i < len; ++i )
  224. {
  225. if ( fbuf[i] > p.max )
  226. p.max = fbuf[i];
  227. if ( fbuf[i] < p.min )
  228. p.min = fbuf[i];
  229. }
  230. fwrite( &p, sizeof( Peak ), 1, fp );
  231. }
  232. while ( len == chunksize );
  233. fclose( fp );
  234. sf_close( in );
  235. delete fbuf;
  236. }