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.

387 lines
9.2KB

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