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.

172 lines
4.2KB

  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 "Audio_File.H"
  19. #include "Audio_File_SF.H"
  20. #include "Audio_File_Dummy.H"
  21. #include "const.h"
  22. #include "util/debug.h"
  23. #include "util/Block_Timer.H"
  24. #include <string.h>
  25. std::map <std::string, Audio_File*> Audio_File::_open_files;
  26. Audio_File::~Audio_File ( )
  27. {
  28. DMESSAGE( "Freeing Audio_File object for \"%s\"", _filename );
  29. _open_files[ std::string( _filename ) ] = NULL;
  30. if ( _filename )
  31. free( _filename );
  32. }
  33. const Audio_File::format_desc *
  34. Audio_File::find_format ( const format_desc *fd, const char *name )
  35. {
  36. for ( ; fd->name; ++fd )
  37. if ( ! strcmp( fd->name, name ) )
  38. return fd;
  39. return NULL;
  40. }
  41. void
  42. Audio_File::all_supported_formats ( std::list <const char *> &formats )
  43. {
  44. const format_desc *fd;
  45. fd = Audio_File_SF::supported_formats;
  46. for ( ; fd->name; ++fd )
  47. formats.push_back( fd->name );
  48. }
  49. static bool
  50. is_absolute ( const char *name )
  51. {
  52. return *name == '/';
  53. }
  54. /** return a static pointer to /name/ corrected for relative path. */
  55. const char *Audio_File::realname ( const char *name )
  56. {
  57. static char rname[512];
  58. if ( is_absolute( name ) )
  59. strncpy( rname, name, sizeof( rname ) );
  60. else
  61. snprintf( rname, sizeof( rname ), "sources/%s", name );
  62. return rname;
  63. }
  64. const char *
  65. Audio_File::filename ( void ) const
  66. {
  67. return realname( _filename );
  68. }
  69. /** attempt to open any supported filetype */
  70. Audio_File *
  71. Audio_File::from_file ( const char * filename )
  72. {
  73. Block_Timer timer( "Opened audio file" );
  74. Audio_File *a;
  75. if ( ( a = _open_files[ std::string( filename ) ] ) )
  76. {
  77. ++a->_refs;
  78. return a;
  79. }
  80. if ( ( a = Audio_File_SF::from_file( filename ) ) )
  81. goto done;
  82. // TODO: other formats
  83. DWARNING( "creating dummy source for \"%s\"", filename );
  84. /* FIXME: wrong place for this? */
  85. if ( ( a = Audio_File_Dummy::from_file( filename ) ) )
  86. goto done;
  87. return NULL;
  88. done:
  89. ASSERT( ! _open_files[ std::string( filename ) ], "Programming errror" );
  90. _open_files[ std::string( filename ) ] = a;
  91. a->_refs = 1;
  92. return a;
  93. }
  94. Audio_File *
  95. Audio_File::duplicate ( void )
  96. {
  97. ++_refs;
  98. return this;
  99. }
  100. /** release the resources assoicated with this audio file if no other
  101. * references to it exist */
  102. void
  103. Audio_File::release ( void )
  104. {
  105. if ( --_refs == 0 )
  106. delete this;
  107. }
  108. bool
  109. Audio_File::read_peaks( float fpp, nframes_t start, nframes_t end, int *peaks, Peak **pbuf, int *channels )
  110. {
  111. // Peaks pk;
  112. if ( dummy() )
  113. {
  114. *peaks = (end - start) / fpp;
  115. *channels = 0;
  116. *pbuf = NULL;
  117. return false;
  118. }
  119. else
  120. {
  121. *peaks = 0;
  122. *channels = 0;
  123. *pbuf = NULL;
  124. *peaks = _peaks.fill_buffer( fpp, start, end );
  125. *channels = this->channels();
  126. *pbuf = _peaks.peakbuf();
  127. return true;
  128. }
  129. }