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.

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