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