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.

236 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->_current_read = 0;
  58. c->_filename = strdup( filename );
  59. c->_length = si.frames;
  60. c->_samplerate = si.samplerate;
  61. c->_channels = si.channels;
  62. c->_in = in;
  63. // sf_close( in );
  64. return c;
  65. invalid:
  66. sf_close( in );
  67. return NULL;
  68. }
  69. Audio_File_SF *
  70. Audio_File_SF::create ( const char *filename, nframes_t samplerate, int channels, const char *format )
  71. {
  72. SF_INFO si;
  73. SNDFILE *out;
  74. memset( &si, 0, sizeof( si ) );
  75. const Audio_File::format_desc *fd = Audio_File::find_format( Audio_File_SF::supported_formats, format );
  76. if ( ! fd )
  77. return (Audio_File_SF *)1;
  78. si.samplerate = samplerate;
  79. si.channels = channels;
  80. si.format = fd->id;
  81. char *name;
  82. asprintf( &name, "%s.%s", filename, fd->extension );
  83. // if ( ! ( out = sf_open( name, SFM_RDWR, &si ) ) )
  84. if ( ! ( out = sf_open( name, SFM_WRITE, &si ) ) )
  85. {
  86. printf( "couldn't create soundfile.\n" );
  87. free( name );
  88. return NULL;
  89. }
  90. Audio_File_SF *c = new Audio_File_SF;
  91. c->_filename = name;
  92. c->_length = 0;
  93. c->_samplerate = samplerate;
  94. c->_channels = channels;
  95. c->_in = out;
  96. /* FIXME: 256 ? */
  97. c->_peak_writer = new Peak_Writer( filename, 256, channels );
  98. return c;
  99. }
  100. bool
  101. Audio_File_SF::open ( void )
  102. {
  103. SF_INFO si;
  104. assert( _in == NULL );
  105. memset( &si, 0, sizeof( si ) );
  106. if ( ! ( _in = sf_open( _filename, SFM_READ, &si ) ) )
  107. return false;
  108. _current_read = 0;
  109. _length = si.frames;
  110. _samplerate = si.samplerate;
  111. _channels = si.channels;
  112. // seek( 0 );
  113. return true;
  114. }
  115. void
  116. Audio_File_SF::close ( void )
  117. {
  118. if ( _in )
  119. sf_close( _in );
  120. if ( _peak_writer )
  121. delete _peak_writer;
  122. _in = NULL;
  123. }
  124. void
  125. Audio_File_SF::seek ( nframes_t offset )
  126. {
  127. if ( offset != _current_read )
  128. {
  129. sf_seek( _in, _current_read = offset, SEEK_SET | SFM_READ );
  130. }
  131. }
  132. /* if channels is -1, then all channels are read into buffer
  133. (interleaved). buf should be big enough to hold them all */
  134. nframes_t
  135. Audio_File_SF::read ( sample_t *buf, int channel, nframes_t len )
  136. {
  137. if ( len > 256 * 100 )
  138. printf( "warning: attempt to read an insane number of frames (%lu) from soundfile\n", len );
  139. // printf( "len = %lu, channels = %d\n", len, _channels );
  140. nframes_t rlen;
  141. if ( _channels == 1 || channel == -1 )
  142. rlen = sf_readf_float( _in, buf, len );
  143. else
  144. {
  145. sample_t *tmp = new sample_t[ len * _channels ];
  146. rlen = sf_readf_float( _in, tmp, len );
  147. /* extract the requested channel */
  148. for ( int i = channel; i < rlen * _channels; i += _channels )
  149. *(buf++) = tmp[ i ];
  150. delete[] tmp;
  151. }
  152. _current_read += rlen;
  153. return rlen;
  154. }
  155. /** read samples from /start/ to /end/ into /buf/ */
  156. nframes_t
  157. Audio_File_SF::read ( sample_t *buf, int channel, nframes_t start, nframes_t end )
  158. {
  159. assert( end > start );
  160. // open();
  161. seek( start );
  162. nframes_t len = read( buf, channel, end - start );
  163. // close();
  164. return len;
  165. }
  166. /** write /nframes/ from /buf/ to soundfile. Should be interleaved for
  167. * the appropriate number of channels */
  168. nframes_t
  169. Audio_File_SF::write ( sample_t *buf, nframes_t nframes )
  170. {
  171. _peak_writer->write( buf, nframes );
  172. nframes_t l = sf_writef_float( _in, buf, nframes );
  173. _length += l;
  174. return l;
  175. }