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.

200 lines
4.7KB

  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. try_again:
  85. if ( ( fd = ::open( peakname( filename ), O_RDONLY ) ) < 0 )
  86. {
  87. /* generate peaks here */
  88. if ( make_peaks( filename, 256 ) )
  89. goto try_again;
  90. else
  91. return false;
  92. }
  93. {
  94. struct stat st;
  95. fstat( fd, &st );
  96. _len = st.st_size;
  97. }
  98. _peaks = (peaks*)mmap( NULL, _len, PROT_READ, MAP_SHARED, fd, 0 );
  99. ::close( fd );
  100. if ( _peaks == MAP_FAILED )
  101. printf( "failed to create mapping! " );
  102. _len = (_len - sizeof( int )) / sizeof( Peak );
  103. return true;
  104. }
  105. void
  106. long_to_float( long *buf, int len )
  107. {
  108. for ( int i = len; i--; )
  109. *((float*)buf) = *buf / 32768;
  110. }
  111. bool
  112. Peaks::make_peaks ( const char *filename, int chunksize )
  113. {
  114. SNDFILE *in;
  115. // sox_format_init();
  116. SF_INFO si;
  117. memset( &si, 0, sizeof( si ) );
  118. in = sf_open( filename, SFM_READ, &si );
  119. if ( si.channels != 1 )
  120. abort();
  121. FILE *fp = fopen( peakname( filename ), "w" );
  122. if ( fp == NULL )
  123. {
  124. sf_close( in );
  125. /* return fals */
  126. return false;
  127. }
  128. /* write chunksize first */
  129. fwrite( &chunksize, sizeof( int ), 1, fp );
  130. float *fbuf = new float[ chunksize ];
  131. size_t len;
  132. do {
  133. /* read in a buffer */
  134. len = sf_read_float( in, fbuf, chunksize );
  135. Peak p;
  136. p.max = -1.0;
  137. p.min = 1.0;
  138. for ( int i = 0; i < len; ++i )
  139. {
  140. if ( fbuf[i] > p.max )
  141. p.max = fbuf[i];
  142. if ( fbuf[i] < p.min )
  143. p.min = fbuf[i];
  144. }
  145. fwrite( &p, sizeof( Peak ), 1, fp );
  146. }
  147. while ( len == chunksize );
  148. fclose( fp );
  149. sf_close( in );
  150. }