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.

372 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, 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. const char *
  184. Peaks::peakname ( const char *filename ) const
  185. {
  186. static char file[512];
  187. snprintf( file, 512, "%s.peak", filename );
  188. return (const char*)&file;
  189. }
  190. bool
  191. Peaks::open ( void )
  192. {
  193. const char *filename = _clip->name();
  194. int fd;
  195. make_peaks( 256 );
  196. if ( ( fd = ::open( peakname( filename ), O_RDONLY ) ) < 0 )
  197. return false;
  198. {
  199. struct stat st;
  200. fstat( fd, &st );
  201. _len = st.st_size;
  202. }
  203. ::close( fd );
  204. _len = (_len - sizeof( int )) / sizeof( Peak );
  205. return true;
  206. }
  207. /** returns false if peak file for /filename/ is out of date */
  208. bool
  209. Peaks::current ( void ) const
  210. {
  211. int sfd, pfd;
  212. if ( ( sfd = ::open( _clip->name(), O_RDONLY ) ) < 0 )
  213. return true;
  214. if ( ( pfd = ::open( peakname( _clip->name() ), O_RDONLY ) ) < 0 )
  215. return false;
  216. struct stat sst, pst;
  217. fstat( sfd, &sst );
  218. fstat( pfd, &pst );
  219. close( sfd );
  220. close( pfd );
  221. return sst.st_mtime <= pst.st_mtime;
  222. }
  223. /** build peaks file for /filename/ if necessary */
  224. bool
  225. Peaks::make_peaks ( int chunksize )
  226. {
  227. const char *filename = _clip->name();
  228. if ( current() )
  229. return true;
  230. if ( ! _clip->open() )
  231. return false;
  232. FILE *fp = fopen( peakname( filename ), "w" );
  233. if ( fp == NULL )
  234. {
  235. _clip->close();
  236. return false;
  237. }
  238. /* write chunksize first */
  239. fwrite( &chunksize, sizeof( int ), 1, fp );
  240. Peak peaks[ _clip->channels() ];
  241. size_t len;
  242. do {
  243. len = read_source_peaks( peaks, 1, chunksize );
  244. fwrite( peaks, sizeof( peaks ), 1, fp );
  245. }
  246. while ( len );
  247. _clip->close();
  248. fclose( fp );
  249. return true;
  250. }
  251. /** return normalization factor for range of samples from /start/ to
  252. /end/ (uses known peak data if possible */
  253. /* float */
  254. /* Peaks::normalization_factor( float fpp, nframes_t start, nframes_t end ) const */
  255. /* { */
  256. /* float s; */
  257. /* // fill_buffer( fpp, start, end ); */
  258. /* /\* if ( end - start < _peaks->chunksize * 4 ) *\/ */
  259. /* /\* fill_buffer( _clip->length() / 4, start, end ); *\/ */
  260. /* /\* else *\/ */
  261. /* /\* fill_buffer( _clip->length(), start, end ); *\/ */
  262. /* Peak p = peak( start, end ); */
  263. /* s = 1.0f / fabs( p.max ); */
  264. /* if ( s * p.min < -1.0 ) */
  265. /* s = 1.0f / fabs( p.min ); */
  266. /* return s; */
  267. /* } */