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.

454 lines
10KB

  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. static
  35. const char *
  36. peakname ( const char *filename )
  37. {
  38. static char file[512];
  39. snprintf( file, 512, "%s.peak", filename );
  40. return (const char*)&file;
  41. }
  42. /** Prepare a buffer of peaks from /s/ to /e/ for reading. Must be
  43. * called before any calls to operator[] */
  44. int
  45. Peaks::fill_buffer ( float fpp, nframes_t s, nframes_t e ) const
  46. {
  47. _fpp = fpp;
  48. return read_peaks( s, e, (e - s) / fpp, fpp );
  49. }
  50. /* inline void */
  51. /* Peaks::downsample ( Peak *peaks, int s, int e, float *mhi, float *mlo ) const */
  52. /* { */
  53. /* *mhi = 0; */
  54. /* *mlo = 0; */
  55. /* if ( e > _len ) */
  56. /* e = _len; */
  57. /* for ( int j = s; j < e; j++ ) */
  58. /* { */
  59. /* const float lo = peaks[ j ].min; */
  60. /* const float hi = peaks[ j ].max; */
  61. /* if ( hi > *mhi ) */
  62. /* *mhi = hi; */
  63. /* if ( lo < *mlo ) */
  64. /* *mlo = lo; */
  65. /* } */
  66. /* } */
  67. int
  68. Peaks::read_peakfile_peaks ( Peak *peaks, nframes_t s, int npeaks, int chunksize ) const
  69. {
  70. FILE *fp;
  71. if ( ! ( fp = fopen( peakname( _clip->name() ), "r" ) ) )
  72. {
  73. printf( "failed to open peak file!\n" );
  74. return 0;
  75. }
  76. /* get chunk size of peak file */
  77. int pfchunksize = 0;
  78. if ( fread( &pfchunksize, sizeof( int ), 1, fp ) != 1 )
  79. {
  80. printf( "invalid peak file!\n" );
  81. return 0;
  82. }
  83. int channels = _clip->channels();
  84. const int ratio = chunksize / pfchunksize;
  85. /* locate to start position */
  86. if ( fseek( fp, (s * channels / pfchunksize) * sizeof( Peak ), SEEK_CUR ) )
  87. /* failed to seek... peaks not ready? */
  88. return 0;
  89. if ( ratio == 1 )
  90. {
  91. int len = fread( peaks, sizeof( Peak ) * channels, npeaks, fp );
  92. fclose( fp );
  93. return len;
  94. }
  95. Peak *pbuf = new Peak[ ratio * channels ];
  96. size_t len = 0;
  97. int i;
  98. for ( i = 0; i < npeaks; ++i )
  99. {
  100. /* read in a buffer */
  101. len = fread( pbuf, sizeof( Peak ) * channels, ratio, fp );
  102. Peak *pk = peaks + (i * channels);
  103. /* get the peak for each channel */
  104. for ( int j = 0; j < channels; ++j )
  105. {
  106. Peak *p = &pk[ j ];
  107. p->min = 0;
  108. p->max = 0;
  109. const Peak *pb = pbuf + j;
  110. for ( int k = len * channels; k--; pb += channels )
  111. {
  112. if ( pb->max > p->max )
  113. p->max = pb->max;
  114. if ( pb->min < p->min )
  115. p->min = pb->min;
  116. }
  117. }
  118. if ( len < ratio )
  119. break;
  120. }
  121. delete[] pbuf;
  122. fclose( fp );
  123. return i;
  124. }
  125. int
  126. Peaks::read_source_peaks ( Peak *peaks, int npeaks, int chunksize ) const
  127. {
  128. int channels = _clip->channels();
  129. sample_t *fbuf = new sample_t[ chunksize * channels ];
  130. size_t len;
  131. int i;
  132. for ( i = 0; i < npeaks; ++i )
  133. {
  134. /* read in a buffer */
  135. len = _clip->read( fbuf, -1, chunksize );
  136. Peak *pk = peaks + (i * channels);
  137. /* get the peak for each channel */
  138. for ( int j = 0; j < channels; ++j )
  139. {
  140. Peak &p = pk[ j ];
  141. p.min = 0;
  142. p.max = 0;
  143. for ( int k = j; k < len * channels; k += channels )
  144. {
  145. if ( fbuf[ k ] > p.max )
  146. p.max = fbuf[ k ];
  147. if ( fbuf[ k ] < p.min )
  148. p.min = fbuf[ k ];
  149. }
  150. }
  151. if ( len < chunksize )
  152. break;
  153. }
  154. delete[] fbuf;
  155. return i;
  156. }
  157. int
  158. Peaks::read_source_peaks ( Peak *peaks, nframes_t s, int npeaks, int chunksize ) const
  159. {
  160. // _clip->open();
  161. _clip->seek( s );
  162. int i = read_source_peaks( peaks, npeaks, chunksize );
  163. // _clip->close();
  164. return i;
  165. }
  166. int
  167. Peaks::read_peaks ( nframes_t s, nframes_t e, int npeaks, int chunksize ) const
  168. {
  169. printf( "reading peaks %d @ %d\n", npeaks, chunksize );
  170. if ( _peakbuf.size < npeaks * _clip->channels() )
  171. {
  172. _peakbuf.size = npeaks * _clip->channels();
  173. // printf( "reallocating peak buffer %li\n", _peakbuf.size );
  174. _peakbuf.buf = (peakdata*)realloc( _peakbuf.buf, sizeof( peakdata ) + (_peakbuf.size * sizeof( Peak )) );
  175. }
  176. assert( s >= 0 );
  177. _peakbuf.offset = s;
  178. _peakbuf.buf->chunksize = chunksize;
  179. /* FIXME: compart to (minimum) peakfile chunk size */
  180. if ( chunksize < 256 )
  181. _peakbuf.len = read_source_peaks( _peakbuf.buf->data, s, npeaks, chunksize );
  182. else
  183. _peakbuf.len = read_peakfile_peaks( _peakbuf.buf->data, s, npeaks, chunksize );
  184. return _peakbuf.len;
  185. }
  186. /** Return the peak for the range of samples */
  187. /* Peak & */
  188. /* Peaks::peak ( nframes_t start, nframes_t end ) const */
  189. /* { */
  190. /* /\* Is there a better way to return this? *\/ */
  191. /* static Peak p; */
  192. /* start = (start - _peakbuf.offset) / _peakbuf.buf->chunksize; */
  193. /* end = (end - _peakbuf.offset) / _peakbuf.buf->chunksize; */
  194. /* if ( end > _peakbuf.len ) */
  195. /* end = _peakbuf.len; */
  196. /* downsample( _peakbuf.buf->data, start, end, &p.max, &p.min ); */
  197. /* return p; */
  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. /* } */
  285. Peak_Writer::Peak_Writer ( const char *filename, int chunksize, int channels )
  286. {
  287. _channels = channels;
  288. _chunksize = chunksize;
  289. _peak = new Peak[ channels ];
  290. memset( _peak, 0, sizeof( Peak ) * channels );
  291. if ( ! ( _fp = fopen( peakname( filename ), "w" ) ) )
  292. /* error! */;
  293. write_header();
  294. }
  295. Peak_Writer::~Peak_Writer ( )
  296. {
  297. fclose( _fp );
  298. delete _peak;
  299. }
  300. void
  301. Peak_Writer::write_header ( void )
  302. {
  303. fprintf( _fp, "NON-PEAKS%2d%2d", VERSION_MAJOR, VERSION_MINOR );
  304. int data[] = { _chunksize, _channels, sizeof( Peak ) };
  305. fwrite( &data, sizeof( data ), 1, _fp );
  306. }
  307. /** append peaks for samples in /buf/ to peakfile */
  308. void
  309. Peak_Writer::write ( sample_t *buf, nframes_t nframes )
  310. {
  311. for ( int i = _chunksize; nframes--; --i, buf += _channels )
  312. {
  313. for ( int j = 0; j < _channels; ++j )
  314. {
  315. Peak *p = _peak + j;
  316. if ( *buf > p->max )
  317. p->max = *buf;
  318. if ( *buf < p->min )
  319. p->min = *buf;
  320. }
  321. if ( ! i )
  322. {
  323. fwrite( _peak, sizeof( Peak ), _channels, _fp );
  324. memset( _peak, 0, sizeof( Peak ) * _channels );
  325. i = _chunksize;
  326. }
  327. }
  328. }