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.

199 lines
4.8KB

  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 "debug.h"
  23. #include "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. if ( _path )
  33. free( _path );
  34. }
  35. const Audio_File::format_desc *
  36. Audio_File::find_format ( const format_desc *fd, const char *name )
  37. {
  38. for ( ; fd->name; ++fd )
  39. if ( ! strcmp( fd->name, name ) )
  40. return fd;
  41. return NULL;
  42. }
  43. void
  44. Audio_File::all_supported_formats ( std::list <const char *> &formats )
  45. {
  46. const format_desc *fd;
  47. fd = Audio_File_SF::supported_formats;
  48. for ( ; fd->name; ++fd )
  49. formats.push_back( fd->name );
  50. }
  51. static bool
  52. is_absolute ( const char *name )
  53. {
  54. return *name == '/';
  55. }
  56. /** return pointer to /name/ corrected for relative path. */
  57. char *Audio_File::path ( const char *name )
  58. {
  59. char *path = 0;
  60. if ( is_absolute( name ) )
  61. path = strdup( name );
  62. else
  63. asprintf( &path, "sources/%s", name );
  64. return path;
  65. }
  66. const char *
  67. Audio_File::filename ( void ) const
  68. {
  69. return _path;
  70. }
  71. static bool is_poor_seeker ( const char * filename )
  72. {
  73. if ( ( strlen(filename) > 4 &&
  74. ! strcasecmp( &filename[strlen(filename)-4], ".ogg" ) )
  75. ||
  76. ( strlen(filename) > 5 &&
  77. ! strcasecmp( &filename[strlen(filename)-5], ".flac" ) )
  78. )
  79. {
  80. return true;
  81. }
  82. return false;
  83. }
  84. /** attempt to open any supported filetype */
  85. Audio_File *
  86. Audio_File::from_file ( const char * filename )
  87. {
  88. Block_Timer timer( "Opened audio file" );
  89. Audio_File *a;
  90. if ( is_poor_seeker(filename) )
  91. {
  92. /* OGG and FLAC have poor seek performance, so they require
  93. * separate file descriptors to be useful */
  94. }
  95. else
  96. {
  97. /* WAV are quick enough to seek that we can save
  98. * filedescriptors by sharing them between regions */
  99. if ( ( a = _open_files[ std::string( filename ) ] ) )
  100. {
  101. ++a->_refs;
  102. return a;
  103. }
  104. }
  105. if ( ( a = Audio_File_SF::from_file( filename ) ) )
  106. goto done;
  107. // TODO: other formats
  108. DWARNING( "creating dummy source for \"%s\"", filename );
  109. /* FIXME: wrong place for this? */
  110. if ( ( a = Audio_File_Dummy::from_file( filename ) ) )
  111. goto done;
  112. return NULL;
  113. done:
  114. /* ASSERT( ! _open_files[ std::string( filename ) ], "Programming errror" ); */
  115. _open_files[ std::string( filename ) ] = a;
  116. a->_refs = 1;
  117. return a;
  118. }
  119. Audio_File *
  120. Audio_File::duplicate ( void )
  121. {
  122. if ( is_poor_seeker( _filename ) )
  123. {
  124. return from_file(_filename);
  125. }
  126. else
  127. {
  128. ++_refs;
  129. return this;
  130. }
  131. }
  132. /** release the resources assoicated with this audio file if no other
  133. * references to it exist */
  134. void
  135. Audio_File::release ( void )
  136. {
  137. if ( --_refs == 0 )
  138. delete this;
  139. }
  140. bool
  141. Audio_File::read_peaks( float fpp, nframes_t start, nframes_t end, int *peaks, Peak **pbuf, int *channels )
  142. {
  143. *peaks = 0;
  144. *channels = 0;
  145. *pbuf = NULL;
  146. if ( dummy() )
  147. return false;
  148. else
  149. {
  150. *peaks = _peaks.fill_buffer( fpp, start, end );
  151. *channels = this->channels();
  152. *pbuf = _peaks.peakbuf();
  153. return true;
  154. }
  155. }