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.

247 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. #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_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. c->_peaks.prepare_for_writing();
  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. _in = NULL;
  121. }
  122. void
  123. Audio_File_SF::seek ( nframes_t offset )
  124. {
  125. lock();
  126. if ( offset != _current_read )
  127. sf_seek( _in, _current_read = offset, SEEK_SET | SFM_READ );
  128. unlock();
  129. }
  130. /* if channels is -1, then all channels are read into buffer
  131. (interleaved). buf should be big enough to hold them all */
  132. nframes_t
  133. Audio_File_SF::read ( sample_t *buf, int channel, nframes_t len )
  134. {
  135. if ( len > 256 * 100 )
  136. WARNING( "warning: attempt to read an insane number of frames (%lu) from soundfile\n", (unsigned long)len );
  137. // printf( "len = %lu, channels = %d\n", len, _channels );
  138. lock();
  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. unlock();
  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. lock();
  161. // open();
  162. seek( start );
  163. nframes_t len = read( buf, channel, end - start );
  164. unlock();
  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. _peaks.write( buf, nframes );
  174. // lock();
  175. nframes_t l = sf_writef_float( _in, buf, nframes );
  176. _length += l;
  177. // unlock();
  178. return l;
  179. }