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.

120 lines
3.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. void
  29. Peaks::downsample ( int s, int e, float *mhi, float *mlo ) const
  30. {
  31. *mhi = -1.0;
  32. *mlo = 1.0;
  33. if ( e > _len )
  34. e = _len;
  35. for ( int j = s; j < e; j++ )
  36. {
  37. const float lo = _peaks->data[ j ].min;
  38. const float hi = _peaks->data[ j ].max;
  39. if ( hi > *mhi )
  40. *mhi = hi;
  41. if ( lo < *mlo )
  42. *mlo = lo;
  43. }
  44. }
  45. void
  46. Peaks::read ( int X, float *hi, float *lo ) const
  47. {
  48. int start = X * timeline.fpp;
  49. int end = (X + 1) * timeline.fpp;
  50. downsample( start, end, hi, lo );
  51. }
  52. /* virtual array. Index is a Pixel value, and it returns the
  53. * (resampled) peaks for that pixel based on the current timeline
  54. * zoom. */
  55. Peak &
  56. Peaks::operator[] ( int X ) const
  57. {
  58. /* Is there a better way to return this? */
  59. static Peak p;
  60. if ( timeline.fpp < _peaks->chunksize )
  61. {
  62. printf( "we need to a smaller chunksize! examine the source!\n" );
  63. }
  64. int start = timeline.x_to_ts( X ) / _peaks->chunksize;
  65. int end = timeline.x_to_ts( X + 1 ) / _peaks->chunksize;
  66. /* int start = X * timeline.fpp; */
  67. /* int end = (X + 1) * timeline.fpp; */
  68. downsample( start, end, &p.max, &p.min );
  69. return p;
  70. }
  71. bool
  72. Peaks::open ( const char *filename )
  73. {
  74. char file[512];
  75. snprintf( file, 512, "%s.peak", filename );
  76. int fd;
  77. if ( ( fd = ::open( file, O_RDONLY ) ) < 0 )
  78. {
  79. /* generate peaks here */
  80. }
  81. {
  82. struct stat st;
  83. fstat( fd, &st );
  84. _len = st.st_size;
  85. }
  86. _peaks = (peaks*)mmap( NULL, _len, PROT_READ, MAP_SHARED, fd, 0 );
  87. ::close( fd );
  88. if ( _peaks == MAP_FAILED )
  89. printf( "failed to create mapping! " );
  90. _len = (_len - sizeof( int )) / sizeof( Peak );
  91. return true;
  92. }