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.

392 lines
9.3KB

  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. const Peak *pb = pbuf + j;
  102. for ( int k = len * channels; k--; pb += channels )
  103. {
  104. const Peak pp = *pb;
  105. if ( pp.max > p.max )
  106. p.max = pp.max;
  107. if ( pp.min < p.min )
  108. p.min = pp.min;
  109. }
  110. }
  111. if ( len < ratio )
  112. break;
  113. }
  114. delete[] pbuf;
  115. fclose( fp );
  116. return i;
  117. }
  118. int
  119. Peaks::read_source_peaks ( Peak *peaks, int npeaks, int chunksize ) const
  120. {
  121. int channels = _clip->channels();
  122. sample_t *fbuf = new sample_t[ chunksize * channels ];
  123. size_t len;
  124. int i;
  125. for ( i = 0; i < npeaks; ++i )
  126. {
  127. /* read in a buffer */
  128. len = _clip->read( fbuf, -1, chunksize );
  129. Peak *pk = peaks + (i * channels);
  130. /* get the peak for each channel */
  131. for ( int j = 0; j < channels; ++j )
  132. {
  133. Peak &p = pk[ j ];
  134. p.min = 0;
  135. p.max = 0;
  136. for ( int k = j; k < len * channels; k += channels )
  137. {
  138. if ( fbuf[ k ] > p.max )
  139. p.max = fbuf[ k ];
  140. if ( fbuf[ k ] < p.min )
  141. p.min = fbuf[ k ];
  142. }
  143. }
  144. if ( len < chunksize )
  145. break;
  146. }
  147. delete[] fbuf;
  148. return i;
  149. }
  150. int
  151. Peaks::read_source_peaks ( Peak *peaks, nframes_t s, int npeaks, int chunksize ) const
  152. {
  153. // _clip->open();
  154. _clip->seek( s );
  155. int i = read_source_peaks( peaks, npeaks, chunksize );
  156. // _clip->close();
  157. return i;
  158. }
  159. int
  160. Peaks::read_peaks ( nframes_t s, nframes_t e, int npeaks, int chunksize ) const
  161. {
  162. printf( "reading peaks %d @ %d\n", npeaks, chunksize );
  163. if ( _peakbuf.size < npeaks * _clip->channels() )
  164. {
  165. _peakbuf.size = npeaks * _clip->channels();
  166. // printf( "reallocating peak buffer %li\n", _peakbuf.size );
  167. _peakbuf.buf = (peakdata*)realloc( _peakbuf.buf, sizeof( peakdata ) + (_peakbuf.size * sizeof( Peak )) );
  168. }
  169. assert( s >= 0 );
  170. _peakbuf.offset = s;
  171. _peakbuf.buf->chunksize = chunksize;
  172. /* FIXME: compart to (minimum) peakfile chunk size */
  173. if ( chunksize < 256 )
  174. _peakbuf.len = read_source_peaks( _peakbuf.buf->data, s, npeaks, chunksize );
  175. else
  176. _peakbuf.len = read_peakfile_peaks( _peakbuf.buf->data, s, npeaks, chunksize );
  177. return _peakbuf.len;
  178. }
  179. /** Return the peak for the range of samples */
  180. /* Peak & */
  181. /* Peaks::peak ( nframes_t start, nframes_t end ) const */
  182. /* { */
  183. /* /\* Is there a better way to return this? *\/ */
  184. /* static Peak p; */
  185. /* start = (start - _peakbuf.offset) / _peakbuf.buf->chunksize; */
  186. /* end = (end - _peakbuf.offset) / _peakbuf.buf->chunksize; */
  187. /* if ( end > _peakbuf.len ) */
  188. /* end = _peakbuf.len; */
  189. /* downsample( _peakbuf.buf->data, start, end, &p.max, &p.min ); */
  190. /* return p; */
  191. /* } */
  192. const char *
  193. Peaks::peakname ( const char *filename ) const
  194. {
  195. static char file[512];
  196. snprintf( file, 512, "%s.peak", filename );
  197. return (const char*)&file;
  198. }
  199. bool
  200. Peaks::open ( void )
  201. {
  202. const char *filename = _clip->name();
  203. int fd;
  204. if ( ! current() )
  205. /* Build peaks asyncronously */
  206. if ( ! fork() )
  207. exit( 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. /* FIXME: we need to work out a way to run this in another thread and
  236. possibly stream back the data to the GUI */
  237. /** build peaks file for /filename/ if necessary */
  238. bool
  239. Peaks::make_peaks ( int chunksize )
  240. {
  241. const char *filename = _clip->name();
  242. if ( current() )
  243. return true;
  244. _clip->seek( 0 );
  245. /* if ( ! _clip->open() ) */
  246. /* return false; */
  247. FILE *fp = fopen( peakname( filename ), "w" );
  248. if ( fp == NULL )
  249. {
  250. // _clip->close();
  251. return false;
  252. }
  253. /* write chunksize first */
  254. fwrite( &chunksize, sizeof( int ), 1, fp );
  255. Peak peaks[ _clip->channels() ];
  256. size_t len;
  257. do {
  258. len = read_source_peaks( peaks, 1, chunksize );
  259. fwrite( peaks, sizeof( peaks ), 1, fp );
  260. /* FIXME: GUI code shouldn't be here! */
  261. // Fl::check();
  262. }
  263. while ( len );
  264. // _clip->close();
  265. fclose( fp );
  266. return true;
  267. }
  268. /** return normalization factor for range of samples from /start/ to
  269. /end/ (uses known peak data if possible */
  270. /* float */
  271. /* Peaks::normalization_factor( float fpp, nframes_t start, nframes_t end ) const */
  272. /* { */
  273. /* float s; */
  274. /* // fill_buffer( fpp, start, end ); */
  275. /* /\* if ( end - start < _peaks->chunksize * 4 ) *\/ */
  276. /* /\* fill_buffer( _clip->length() / 4, start, end ); *\/ */
  277. /* /\* else *\/ */
  278. /* /\* fill_buffer( _clip->length(), start, end ); *\/ */
  279. /* Peak p = peak( start, end ); */
  280. /* s = 1.0f / fabs( p.max ); */
  281. /* if ( s * p.min < -1.0 ) */
  282. /* s = 1.0f / fabs( p.min ); */
  283. /* return s; */
  284. /* } */