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.

383 lines
8.7KB

  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, nframes_t s, 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. _clip->open();
  118. _clip->seek( s );
  119. int i;
  120. for ( i = 0; i < npeaks; ++i )
  121. {
  122. /* read in a buffer */
  123. len = _clip->read( fbuf, -1, chunksize );
  124. Peak *pk = peaks + (i * channels);
  125. /* get the peak for each channel */
  126. for ( int j = 0; j < channels; ++j )
  127. {
  128. Peak &p = pk[ j ];
  129. p.min = 0;
  130. p.max = 0;
  131. for ( int k = j; k < len * channels; k += channels )
  132. {
  133. if ( fbuf[ k ] > p.max )
  134. p.max = fbuf[ k ];
  135. if ( fbuf[ k ] < p.min )
  136. p.min = fbuf[ k ];
  137. }
  138. }
  139. if ( len < chunksize )
  140. break;
  141. }
  142. delete fbuf;
  143. _clip->close();
  144. return i;
  145. }
  146. void
  147. Peaks::read_peaks ( int s, int e, int npeaks, int chunksize ) const
  148. {
  149. printf( "reading peaks %d @ %d\n", npeaks, chunksize );
  150. if ( _peakbuf.size < npeaks * _clip->channels() )
  151. {
  152. _peakbuf.size = npeaks * _clip->channels();
  153. // printf( "reallocating peak buffer %li\n", _peakbuf.size );
  154. _peakbuf.buf = (peakdata*)realloc( _peakbuf.buf, sizeof( peakdata ) + (_peakbuf.size * sizeof( Peak )) );
  155. }
  156. _peakbuf.offset = s;
  157. _peakbuf.buf->chunksize = chunksize;
  158. /* FIXME: compart to (minimum) peakfile chunk size */
  159. if ( chunksize < 256 )
  160. _peakbuf.len = read_source_peaks( _peakbuf.buf->data, s, npeaks, chunksize );
  161. else
  162. _peakbuf.len = read_peakfile_peaks( _peakbuf.buf->data, s, npeaks, chunksize );
  163. }
  164. /** Return the peak for the range of samples */
  165. Peak &
  166. Peaks::peak ( nframes_t start, nframes_t end ) const
  167. {
  168. /* Is there a better way to return this? */
  169. static Peak p;
  170. start = (start - _peakbuf.offset) / _peakbuf.buf->chunksize;
  171. end = (end - _peakbuf.offset) / _peakbuf.buf->chunksize;
  172. if ( end > _peakbuf.len )
  173. end = _peakbuf.len;
  174. downsample( _peakbuf.buf->data, start, end, &p.max, &p.min );
  175. return p;
  176. }
  177. /* virtual array. Index is a Pixel value, and it returns the
  178. * (resampled) peaks for that pixel based on the current timeline
  179. * zoom. */
  180. /* Peak & */
  181. /* Peaks::operator[] ( int X ) const */
  182. /* { */
  183. /* Peak p; */
  184. /* p.min = 0; */
  185. /* p.max = 0; */
  186. /* return p; */
  187. /* // return peak( timeline->x_to_ts( X ), timeline->x_to_ts( X + 1 ) ); */
  188. /* } */
  189. const char *
  190. Peaks::peakname ( const char *filename ) const
  191. {
  192. static char file[512];
  193. snprintf( file, 512, "%s.peak", filename );
  194. return (const char*)&file;
  195. }
  196. bool
  197. Peaks::open ( void )
  198. {
  199. const char *filename = _clip->name();
  200. int fd;
  201. make_peaks( 256 );
  202. if ( ( fd = ::open( peakname( filename ), O_RDONLY ) ) < 0 )
  203. return false;
  204. {
  205. struct stat st;
  206. fstat( fd, &st );
  207. _len = st.st_size;
  208. }
  209. ::close( fd );
  210. _len = (_len - sizeof( int )) / sizeof( Peak );
  211. return true;
  212. }
  213. /** returns false if peak file for /filename/ is out of date */
  214. bool
  215. Peaks::current ( void ) const
  216. {
  217. int sfd, pfd;
  218. if ( ( sfd = ::open( _clip->name(), O_RDONLY ) ) < 0 )
  219. return true;
  220. if ( ( pfd = ::open( peakname( _clip->name() ), O_RDONLY ) ) < 0 )
  221. return false;
  222. struct stat sst, pst;
  223. fstat( sfd, &sst );
  224. fstat( pfd, &pst );
  225. close( sfd );
  226. close( pfd );
  227. return sst.st_mtime <= pst.st_mtime;
  228. }
  229. /** build peaks file for /filename/ if necessary */
  230. bool
  231. Peaks::make_peaks ( int chunksize )
  232. {
  233. const char *filename = _clip->name();
  234. if ( current() )
  235. return true;
  236. if ( ! _clip->open() )
  237. return false;
  238. FILE *fp = fopen( peakname( filename ), "w" );
  239. if ( fp == NULL )
  240. {
  241. _clip->close();
  242. return false;
  243. }
  244. /* write chunksize first */
  245. fwrite( &chunksize, sizeof( int ), 1, fp );
  246. Peak peaks[ _clip->channels() ];
  247. size_t len;
  248. nframes_t s = 0;
  249. do {
  250. len = read_source_peaks( peaks, s, 1, chunksize );
  251. s += len * chunksize;
  252. fwrite( peaks, sizeof( peaks ), 1, fp );
  253. }
  254. while ( len );
  255. _clip->close();
  256. fclose( fp );
  257. return true;
  258. }
  259. /** return normalization factor for range of samples from /start/ to
  260. /end/ (uses known peak data if possible */
  261. float
  262. Peaks::normalization_factor( float fpp, nframes_t start, nframes_t end ) const
  263. {
  264. float s;
  265. // fill_buffer( fpp, start, end );
  266. /* if ( end - start < _peaks->chunksize * 4 ) */
  267. /* fill_buffer( _clip->length() / 4, start, end ); */
  268. /* else */
  269. /* fill_buffer( _clip->length(), start, end ); */
  270. Peak p = peak( start, end );
  271. s = 1.0f / fabs( p.max );
  272. if ( s * p.min < -1.0 )
  273. s = 1.0f / fabs( p.min );
  274. return s;
  275. }