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.

379 lines
9.0KB

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