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.

170 lines
4.1KB

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