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.

237 lines
6.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_SF.H"
  19. // #include "Timeline.H"
  20. #include <sndfile.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <assert.h>
  24. #include "Peaks.H"
  25. // #define HAS_SF_FORMAT_VORBIS
  26. const Audio_File::format_desc Audio_File_SF::supported_formats[] =
  27. {
  28. { "Wav 24", "wav", SF_FORMAT_WAV | SF_FORMAT_PCM_24 | SF_ENDIAN_FILE },
  29. { "Wav 16", "wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16 | SF_ENDIAN_FILE },
  30. { "Wav f32", "wav", SF_FORMAT_WAV | SF_FORMAT_FLOAT | SF_ENDIAN_FILE },
  31. { "Au 24", "au", SF_FORMAT_AU | SF_FORMAT_PCM_24 | SF_ENDIAN_FILE },
  32. { "Au 16", "au", SF_FORMAT_AU | SF_FORMAT_PCM_16 | SF_ENDIAN_FILE },
  33. { "FLAC", "flac", SF_FORMAT_FLAC | SF_FORMAT_PCM_24 },
  34. #ifdef HAS_SF_FORMAT_VORBIS
  35. { "Ogg Vorbis", "ogg", SF_FORMAT_OGG | SF_FORMAT_VORBIS | SF_FORMAT_PCM_16 },
  36. #endif
  37. { 0, 0 }
  38. };
  39. Audio_File_SF *
  40. Audio_File_SF::from_file ( const char *filename )
  41. {
  42. SNDFILE *in;
  43. SF_INFO si;
  44. Audio_File_SF *c = NULL;
  45. memset( &si, 0, sizeof( si ) );
  46. if ( ! ( in = sf_open( filename, SFM_READ, &si ) ) )
  47. {
  48. printf( "couldn't open file\n" );
  49. return NULL;
  50. }
  51. /* if ( si.samplerate != timeline->sample_rate() ) */
  52. /* { */
  53. /* printf( "error: samplerate mismatch!\n" ); */
  54. /* goto invalid; */
  55. /* } */
  56. c = new Audio_File_SF;
  57. c->_peak_writer = NULL;
  58. c->_current_read = 0;
  59. c->_filename = strdup( filename );
  60. c->_length = si.frames;
  61. c->_samplerate = si.samplerate;
  62. c->_channels = si.channels;
  63. c->_in = in;
  64. // sf_close( in );
  65. return c;
  66. invalid:
  67. sf_close( in );
  68. return NULL;
  69. }
  70. Audio_File_SF *
  71. Audio_File_SF::create ( const char *filename, nframes_t samplerate, int channels, const char *format )
  72. {
  73. SF_INFO si;
  74. SNDFILE *out;
  75. memset( &si, 0, sizeof( si ) );
  76. const Audio_File::format_desc *fd = Audio_File::find_format( Audio_File_SF::supported_formats, format );
  77. if ( ! fd )
  78. return (Audio_File_SF *)1;
  79. si.samplerate = samplerate;
  80. si.channels = channels;
  81. si.format = fd->id;
  82. char *name;
  83. asprintf( &name, "%s.%s", filename, fd->extension );
  84. // if ( ! ( out = sf_open( name, SFM_RDWR, &si ) ) )
  85. if ( ! ( out = sf_open( name, SFM_WRITE, &si ) ) )
  86. {
  87. printf( "couldn't create soundfile.\n" );
  88. free( name );
  89. return NULL;
  90. }
  91. Audio_File_SF *c = new Audio_File_SF;
  92. c->_filename = name;
  93. c->_length = 0;
  94. c->_samplerate = samplerate;
  95. c->_channels = channels;
  96. c->_in = out;
  97. /* FIXME: 256 ? */
  98. c->_peak_writer = new Peak_Writer( name, 256, channels );
  99. return c;
  100. }
  101. bool
  102. Audio_File_SF::open ( void )
  103. {
  104. SF_INFO si;
  105. assert( _in == NULL );
  106. memset( &si, 0, sizeof( si ) );
  107. if ( ! ( _in = sf_open( _filename, SFM_READ, &si ) ) )
  108. return false;
  109. _current_read = 0;
  110. _length = si.frames;
  111. _samplerate = si.samplerate;
  112. _channels = si.channels;
  113. // seek( 0 );
  114. return true;
  115. }
  116. void
  117. Audio_File_SF::close ( void )
  118. {
  119. if ( _in )
  120. sf_close( _in );
  121. if ( _peak_writer )
  122. delete _peak_writer;
  123. _in = NULL;
  124. }
  125. void
  126. Audio_File_SF::seek ( nframes_t offset )
  127. {
  128. if ( offset != _current_read )
  129. {
  130. sf_seek( _in, _current_read = offset, SEEK_SET | SFM_READ );
  131. }
  132. }
  133. /* if channels is -1, then all channels are read into buffer
  134. (interleaved). buf should be big enough to hold them all */
  135. nframes_t
  136. Audio_File_SF::read ( sample_t *buf, int channel, nframes_t len )
  137. {
  138. if ( len > 256 * 100 )
  139. printf( "warning: attempt to read an insane number of frames (%lu) from soundfile\n", len );
  140. // printf( "len = %lu, channels = %d\n", len, _channels );
  141. nframes_t rlen;
  142. if ( _channels == 1 || channel == -1 )
  143. rlen = sf_readf_float( _in, buf, len );
  144. else
  145. {
  146. sample_t *tmp = new sample_t[ len * _channels ];
  147. rlen = sf_readf_float( _in, tmp, len );
  148. /* extract the requested channel */
  149. for ( int i = channel; i < rlen * _channels; i += _channels )
  150. *(buf++) = tmp[ i ];
  151. delete[] tmp;
  152. }
  153. _current_read += rlen;
  154. return rlen;
  155. }
  156. /** read samples from /start/ to /end/ into /buf/ */
  157. nframes_t
  158. Audio_File_SF::read ( sample_t *buf, int channel, nframes_t start, nframes_t end )
  159. {
  160. assert( end > start );
  161. // open();
  162. seek( start );
  163. nframes_t len = read( buf, channel, end - start );
  164. // close();
  165. return len;
  166. }
  167. /** write /nframes/ from /buf/ to soundfile. Should be interleaved for
  168. * the appropriate number of channels */
  169. nframes_t
  170. Audio_File_SF::write ( sample_t *buf, nframes_t nframes )
  171. {
  172. _peak_writer->write( buf, nframes );
  173. nframes_t l = sf_writef_float( _in, buf, nframes );
  174. _length += l;
  175. return l;
  176. }