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.

107 lines
3.0KB

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