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.

235 lines
6.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_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. c->_peaks.prepare_for_writing();
  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. _in = NULL;
  122. }
  123. void
  124. Audio_File_SF::seek ( nframes_t offset )
  125. {
  126. if ( offset != _current_read )
  127. {
  128. sf_seek( _in, _current_read = offset, SEEK_SET | SFM_READ );
  129. }
  130. }
  131. /* if channels is -1, then all channels are read into buffer
  132. (interleaved). buf should be big enough to hold them all */
  133. nframes_t
  134. Audio_File_SF::read ( sample_t *buf, int channel, nframes_t len )
  135. {
  136. if ( len > 256 * 100 )
  137. WARNING( "warning: attempt to read an insane number of frames (%lu) from soundfile\n", (unsigned long)len );
  138. // printf( "len = %lu, channels = %d\n", len, _channels );
  139. nframes_t rlen;
  140. if ( _channels == 1 || channel == -1 )
  141. rlen = sf_readf_float( _in, buf, len );
  142. else
  143. {
  144. sample_t *tmp = new sample_t[ len * _channels ];
  145. rlen = sf_readf_float( _in, tmp, len );
  146. /* extract the requested channel */
  147. for ( unsigned int i = channel; i < rlen * _channels; i += _channels )
  148. *(buf++) = tmp[ i ];
  149. delete[] tmp;
  150. }
  151. _current_read += rlen;
  152. return rlen;
  153. }
  154. /** read samples from /start/ to /end/ into /buf/ */
  155. nframes_t
  156. Audio_File_SF::read ( sample_t *buf, int channel, nframes_t start, nframes_t end )
  157. {
  158. assert( end > start );
  159. // open();
  160. seek( start );
  161. nframes_t len = read( buf, channel, end - start );
  162. // close();
  163. return len;
  164. }
  165. /** write /nframes/ from /buf/ to soundfile. Should be interleaved for
  166. * the appropriate number of channels */
  167. nframes_t
  168. Audio_File_SF::write ( sample_t *buf, nframes_t nframes )
  169. {
  170. _peaks.write( buf, nframes );
  171. nframes_t l = sf_writef_float( _in, buf, nframes );
  172. _length += l;
  173. return l;
  174. }