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.

239 lines
6.3KB

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