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.

389 lines
8.8KB

  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. int
  36. Peaks::fill_buffer ( float fpp, int s, int e ) const
  37. {
  38. _fpp = fpp;
  39. read_peaks( s, e, (e - s) / fpp, fpp );
  40. /* FIXME: are we *SURE* we got them all? */
  41. return (e - s) / fpp;
  42. }
  43. inline void
  44. Peaks::downsample ( Peak *peaks, int s, int e, float *mhi, float *mlo ) const
  45. {
  46. *mhi = 0;
  47. *mlo = 0;
  48. if ( e > _len )
  49. e = _len;
  50. for ( int j = s; j < e; j++ )
  51. {
  52. const float lo = peaks[ j ].min;
  53. const float hi = peaks[ j ].max;
  54. if ( hi > *mhi )
  55. *mhi = hi;
  56. if ( lo < *mlo )
  57. *mlo = lo;
  58. }
  59. }
  60. int
  61. Peaks::read_peakfile_peaks ( Peak *peaks, nframes_t s, int npeaks, int chunksize ) const
  62. {
  63. FILE *fp;
  64. if ( ! ( fp = fopen( peakname( _clip->name() ), "r" ) ) )
  65. {
  66. printf( "failed to open peak file!" );
  67. return 0;
  68. }
  69. /* get chunk size of peak file */
  70. int pfchunksize;
  71. fread( &pfchunksize, sizeof( int ), 1, fp );
  72. int channels = _clip->channels();
  73. const int ratio = chunksize / pfchunksize;
  74. if ( ratio == 1 )
  75. {
  76. int len = fread( peaks, sizeof( Peak ) * channels, npeaks, fp );
  77. fclose( fp );
  78. return len;
  79. }
  80. Peak *pbuf = new Peak[ ratio * channels ];
  81. /* locate to start position */
  82. fseek( fp, (s * channels / pfchunksize) * sizeof( Peak ), SEEK_CUR );
  83. size_t len;
  84. int i;
  85. for ( i = 0; i < npeaks; ++i )
  86. {
  87. /* read in a buffer */
  88. len = fread( pbuf, sizeof( Peak ) * channels, ratio, fp );
  89. Peak *pk = peaks + (i * channels);
  90. /* get the peak for each channel */
  91. for ( int j = 0; j < channels; ++j )
  92. {
  93. Peak &p = pk[ j ];
  94. p.min = 0;
  95. p.max = 0;
  96. for ( int k = j; k < len * channels; k += channels )
  97. {
  98. if ( pbuf[ k ].max > p.max )
  99. p.max = pbuf[ k ].max;
  100. if ( pbuf[ k ].min < p.min )
  101. p.min = pbuf[ k ].min;
  102. }
  103. }
  104. if ( len < ratio )
  105. break;
  106. }
  107. delete pbuf;
  108. fclose( fp );
  109. return i;
  110. }
  111. int
  112. Peaks::read_source_peaks ( Peak *peaks, int npeaks, int chunksize ) const
  113. {
  114. int channels = _clip->channels();
  115. sample_t *fbuf = new sample_t[ chunksize * channels ];
  116. size_t len;
  117. int i;
  118. for ( i = 0; i < npeaks; ++i )
  119. {
  120. /* read in a buffer */
  121. len = _clip->read( fbuf, -1, chunksize );
  122. Peak *pk = peaks + (i * channels);
  123. /* get the peak for each channel */
  124. for ( int j = 0; j < channels; ++j )
  125. {
  126. Peak &p = pk[ j ];
  127. p.min = 0;
  128. p.max = 0;
  129. for ( int k = j; k < len * channels; k += channels )
  130. {
  131. if ( fbuf[ k ] > p.max )
  132. p.max = fbuf[ k ];
  133. if ( fbuf[ k ] < p.min )
  134. p.min = fbuf[ k ];
  135. }
  136. }
  137. if ( len < chunksize )
  138. break;
  139. }
  140. delete fbuf;
  141. return i;
  142. }
  143. int
  144. Peaks::read_source_peaks ( Peak *peaks, nframes_t s, int npeaks, int chunksize ) const
  145. {
  146. _clip->open();
  147. _clip->seek( s );
  148. int i = read_source_peaks( peaks, npeaks, chunksize );
  149. _clip->close();
  150. return i;
  151. }
  152. void
  153. Peaks::read_peaks ( int s, int e, int npeaks, int chunksize ) const
  154. {
  155. printf( "reading peaks %d @ %d\n", npeaks, chunksize );
  156. if ( _peakbuf.size < npeaks * _clip->channels() )
  157. {
  158. _peakbuf.size = npeaks * _clip->channels();
  159. // printf( "reallocating peak buffer %li\n", _peakbuf.size );
  160. _peakbuf.buf = (peakdata*)realloc( _peakbuf.buf, sizeof( peakdata ) + (_peakbuf.size * sizeof( Peak )) );
  161. }
  162. _peakbuf.offset = s;
  163. _peakbuf.buf->chunksize = chunksize;
  164. /* FIXME: compart to (minimum) peakfile chunk size */
  165. if ( chunksize < 256 )
  166. _peakbuf.len = read_source_peaks( _peakbuf.buf->data, s, npeaks, chunksize );
  167. else
  168. _peakbuf.len = read_peakfile_peaks( _peakbuf.buf->data, s, npeaks, chunksize );
  169. }
  170. /** Return the peak for the range of samples */
  171. Peak &
  172. Peaks::peak ( nframes_t start, nframes_t end ) const
  173. {
  174. /* Is there a better way to return this? */
  175. static Peak p;
  176. start = (start - _peakbuf.offset) / _peakbuf.buf->chunksize;
  177. end = (end - _peakbuf.offset) / _peakbuf.buf->chunksize;
  178. if ( end > _peakbuf.len )
  179. end = _peakbuf.len;
  180. downsample( _peakbuf.buf->data, start, end, &p.max, &p.min );
  181. return p;
  182. }
  183. /* virtual array. Index is a Pixel value, and it returns the
  184. * (resampled) peaks for that pixel based on the current timeline
  185. * zoom. */
  186. /* Peak & */
  187. /* Peaks::operator[] ( int X ) const */
  188. /* { */
  189. /* Peak p; */
  190. /* p.min = 0; */
  191. /* p.max = 0; */
  192. /* return p; */
  193. /* // return peak( timeline->x_to_ts( X ), timeline->x_to_ts( X + 1 ) ); */
  194. /* } */
  195. const char *
  196. Peaks::peakname ( const char *filename ) const
  197. {
  198. static char file[512];
  199. snprintf( file, 512, "%s.peak", filename );
  200. return (const char*)&file;
  201. }
  202. bool
  203. Peaks::open ( void )
  204. {
  205. const char *filename = _clip->name();
  206. int fd;
  207. make_peaks( 256 );
  208. if ( ( fd = ::open( peakname( filename ), O_RDONLY ) ) < 0 )
  209. return false;
  210. {
  211. struct stat st;
  212. fstat( fd, &st );
  213. _len = st.st_size;
  214. }
  215. ::close( fd );
  216. _len = (_len - sizeof( int )) / sizeof( Peak );
  217. return true;
  218. }
  219. /** returns false if peak file for /filename/ is out of date */
  220. bool
  221. Peaks::current ( void ) const
  222. {
  223. int sfd, pfd;
  224. if ( ( sfd = ::open( _clip->name(), O_RDONLY ) ) < 0 )
  225. return true;
  226. if ( ( pfd = ::open( peakname( _clip->name() ), O_RDONLY ) ) < 0 )
  227. return false;
  228. struct stat sst, pst;
  229. fstat( sfd, &sst );
  230. fstat( pfd, &pst );
  231. close( sfd );
  232. close( pfd );
  233. return sst.st_mtime <= pst.st_mtime;
  234. }
  235. /** build peaks file for /filename/ if necessary */
  236. bool
  237. Peaks::make_peaks ( int chunksize )
  238. {
  239. const char *filename = _clip->name();
  240. if ( current() )
  241. return true;
  242. if ( ! _clip->open() )
  243. return false;
  244. FILE *fp = fopen( peakname( filename ), "w" );
  245. if ( fp == NULL )
  246. {
  247. _clip->close();
  248. return false;
  249. }
  250. /* write chunksize first */
  251. fwrite( &chunksize, sizeof( int ), 1, fp );
  252. Peak peaks[ _clip->channels() ];
  253. size_t len;
  254. do {
  255. len = read_source_peaks( peaks, 1, chunksize );
  256. fwrite( peaks, sizeof( peaks ), 1, fp );
  257. }
  258. while ( len );
  259. _clip->close();
  260. fclose( fp );
  261. return true;
  262. }
  263. /** return normalization factor for range of samples from /start/ to
  264. /end/ (uses known peak data if possible */
  265. float
  266. Peaks::normalization_factor( float fpp, nframes_t start, nframes_t end ) const
  267. {
  268. float s;
  269. // fill_buffer( fpp, start, end );
  270. /* if ( end - start < _peaks->chunksize * 4 ) */
  271. /* fill_buffer( _clip->length() / 4, start, end ); */
  272. /* else */
  273. /* fill_buffer( _clip->length(), start, end ); */
  274. Peak p = peak( start, end );
  275. s = 1.0f / fabs( p.max );
  276. if ( s * p.min < -1.0 )
  277. s = 1.0f / fabs( p.min );
  278. return s;
  279. }