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.

223 lines
5.1KB

  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. void
  30. Peaks::downsample ( int s, int e, float *mhi, float *mlo ) const
  31. {
  32. *mhi = -1.0;
  33. *mlo = 1.0;
  34. if ( e > _len )
  35. e = _len;
  36. for ( int j = s; j < e; j++ )
  37. {
  38. const float lo = _peaks->data[ j ].min;
  39. const float hi = _peaks->data[ j ].max;
  40. if ( hi > *mhi )
  41. *mhi = hi;
  42. if ( lo < *mlo )
  43. *mlo = lo;
  44. }
  45. }
  46. void
  47. Peaks::read ( int X, float *hi, float *lo ) const
  48. {
  49. int start = X * timeline.fpp;
  50. int end = (X + 1) * timeline.fpp;
  51. downsample( start, end, hi, lo );
  52. }
  53. /* virtual array. Index is a Pixel value, and it returns the
  54. * (resampled) peaks for that pixel based on the current timeline
  55. * zoom. */
  56. Peak &
  57. Peaks::operator[] ( int X ) const
  58. {
  59. /* Is there a better way to return this? */
  60. static Peak p;
  61. if ( timeline.fpp < _peaks->chunksize )
  62. {
  63. printf( "we need to a smaller chunksize! examine the source!\n" );
  64. }
  65. int start = timeline.x_to_ts( X ) / _peaks->chunksize;
  66. int end = timeline.x_to_ts( X + 1 ) / _peaks->chunksize;
  67. /* int start = X * timeline.fpp; */
  68. /* int end = (X + 1) * timeline.fpp; */
  69. downsample( start, end, &p.max, &p.min );
  70. return p;
  71. }
  72. static
  73. const char *
  74. peakname ( const char *filename )
  75. {
  76. static char file[512];
  77. snprintf( file, 512, "%s.peak", filename );
  78. return (const char*)&file;
  79. }
  80. bool
  81. Peaks::open ( const char *filename )
  82. {
  83. int fd;
  84. make_peaks( filename, 256 );
  85. if ( ( fd = ::open( peakname( filename ), O_RDONLY ) ) < 0 )
  86. return false;
  87. {
  88. struct stat st;
  89. fstat( fd, &st );
  90. _len = st.st_size;
  91. }
  92. _peaks = (peaks*)mmap( NULL, _len, PROT_READ, MAP_SHARED, fd, 0 );
  93. ::close( fd );
  94. if ( _peaks == MAP_FAILED )
  95. printf( "failed to create mapping!\n" );
  96. _len = (_len - sizeof( int )) / sizeof( Peak );
  97. return true;
  98. }
  99. void
  100. long_to_float( long *buf, int len )
  101. {
  102. for ( int i = len; i--; )
  103. *((float*)buf) = *buf / 32768;
  104. }
  105. /** returns false if peak file for /filename/ is out of date */
  106. bool
  107. Peaks::current ( const char *filename )
  108. {
  109. int sfd, pfd;
  110. if ( ( sfd = ::open( filename, O_RDONLY ) ) < 0 )
  111. return true;
  112. if ( ( pfd = ::open( peakname( filename ), O_RDONLY ) ) < 0 )
  113. return false;
  114. struct stat sst, pst;
  115. fstat( sfd, &sst );
  116. fstat( pfd, &pst );
  117. close( sfd );
  118. close( pfd );
  119. return sst.st_mtime <= pst.st_mtime;
  120. }
  121. /** build peaks file for /filename/ if necessary */
  122. bool
  123. Peaks::make_peaks ( const char *filename, int chunksize )
  124. {
  125. if ( current( filename ) )
  126. return true;
  127. SNDFILE *in;
  128. SF_INFO si;
  129. memset( &si, 0, sizeof( si ) );
  130. in = sf_open( filename, SFM_READ, &si );
  131. if ( si.channels != 1 )
  132. abort();
  133. FILE *fp = fopen( peakname( filename ), "w" );
  134. if ( fp == NULL )
  135. {
  136. sf_close( in );
  137. /* return fals */
  138. return false;
  139. }
  140. /* write chunksize first */
  141. fwrite( &chunksize, sizeof( int ), 1, fp );
  142. float *fbuf = new float[ chunksize ];
  143. size_t len;
  144. do {
  145. /* read in a buffer */
  146. len = sf_read_float( in, fbuf, chunksize );
  147. Peak p;
  148. p.max = -1.0;
  149. p.min = 1.0;
  150. for ( int i = 0; i < len; ++i )
  151. {
  152. if ( fbuf[i] > p.max )
  153. p.max = fbuf[i];
  154. if ( fbuf[i] < p.min )
  155. p.min = fbuf[i];
  156. }
  157. fwrite( &p, sizeof( Peak ), 1, fp );
  158. }
  159. while ( len == chunksize );
  160. fclose( fp );
  161. sf_close( in );
  162. }