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.

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