2008-02-17 23:03:31 +01:00
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
/* Copyright (C) 2008 Jonathan Moore Liles */
|
|
|
|
/* */
|
|
|
|
/* This program is free software; you can redistribute it and/or modify it */
|
|
|
|
/* under the terms of the GNU General Public License as published by the */
|
|
|
|
/* Free Software Foundation; either version 2 of the License, or (at your */
|
|
|
|
/* option) any later version. */
|
|
|
|
/* */
|
|
|
|
/* This program is distributed in the hope that it will be useful, but WITHOUT */
|
|
|
|
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
|
|
|
|
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
|
|
|
|
/* more details. */
|
|
|
|
/* */
|
|
|
|
/* You should have received a copy of the GNU General Public License along */
|
|
|
|
/* with This program; see the file COPYING. If not,write to the Free Software */
|
|
|
|
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
2008-02-22 09:19:20 +01:00
|
|
|
#include "Audio_File_SF.H"
|
2008-03-20 09:43:45 +01:00
|
|
|
// #include "Timeline.H"
|
2008-02-17 23:03:31 +01:00
|
|
|
|
|
|
|
#include <sndfile.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2008-04-09 02:05:15 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
2008-04-17 21:19:41 +02:00
|
|
|
#include "Peaks.H"
|
|
|
|
|
2008-04-26 07:33:04 +02:00
|
|
|
// #define HAS_SF_FORMAT_VORBIS
|
|
|
|
|
|
|
|
const Audio_File::format_desc Audio_File_SF::supported_formats[] =
|
|
|
|
{
|
|
|
|
{ "Wav 24", "wav", SF_FORMAT_WAV | SF_FORMAT_PCM_24 | SF_ENDIAN_FILE },
|
|
|
|
{ "Wav 16", "wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16 | SF_ENDIAN_FILE },
|
|
|
|
{ "Wav f32", "wav", SF_FORMAT_WAV | SF_FORMAT_FLOAT | SF_ENDIAN_FILE },
|
|
|
|
{ "Au 24", "au", SF_FORMAT_AU | SF_FORMAT_PCM_24 | SF_ENDIAN_FILE },
|
|
|
|
{ "Au 16", "au", SF_FORMAT_AU | SF_FORMAT_PCM_16 | SF_ENDIAN_FILE },
|
|
|
|
{ "FLAC", "flac", SF_FORMAT_FLAC | SF_FORMAT_PCM_24 },
|
|
|
|
#ifdef HAS_SF_FORMAT_VORBIS
|
|
|
|
{ "Ogg Vorbis", "ogg", SF_FORMAT_OGG | SF_FORMAT_VORBIS | SF_FORMAT_PCM_16 },
|
|
|
|
#endif
|
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
2008-02-22 09:19:20 +01:00
|
|
|
Audio_File_SF *
|
|
|
|
Audio_File_SF::from_file ( const char *filename )
|
2008-02-18 04:37:26 +01:00
|
|
|
{
|
2008-02-17 23:03:31 +01:00
|
|
|
SNDFILE *in;
|
|
|
|
SF_INFO si;
|
|
|
|
|
2008-02-22 09:19:20 +01:00
|
|
|
Audio_File_SF *c = NULL;
|
2008-02-18 04:37:26 +01:00
|
|
|
|
2008-02-17 23:03:31 +01:00
|
|
|
memset( &si, 0, sizeof( si ) );
|
|
|
|
|
2008-02-18 04:37:26 +01:00
|
|
|
if ( ! ( in = sf_open( filename, SFM_READ, &si ) ) )
|
|
|
|
{
|
|
|
|
printf( "couldn't open file\n" );
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-02-17 23:03:31 +01:00
|
|
|
|
2008-03-20 09:43:45 +01:00
|
|
|
/* if ( si.samplerate != timeline->sample_rate() ) */
|
|
|
|
/* { */
|
|
|
|
/* printf( "error: samplerate mismatch!\n" ); */
|
|
|
|
/* goto invalid; */
|
|
|
|
/* } */
|
2008-02-18 04:37:26 +01:00
|
|
|
|
2008-02-22 09:19:20 +01:00
|
|
|
c = new Audio_File_SF;
|
2008-02-17 23:03:31 +01:00
|
|
|
|
2008-04-26 08:33:44 +02:00
|
|
|
c->_peak_writer = NULL;
|
2008-04-10 05:44:02 +02:00
|
|
|
c->_current_read = 0;
|
2008-04-16 10:44:31 +02:00
|
|
|
c->_filename = strdup( filename );
|
|
|
|
c->_length = si.frames;
|
|
|
|
c->_samplerate = si.samplerate;
|
|
|
|
c->_channels = si.channels;
|
2008-02-17 23:03:31 +01:00
|
|
|
|
2008-02-22 19:40:51 +01:00
|
|
|
c->_in = in;
|
|
|
|
// sf_close( in );
|
2008-02-17 23:03:31 +01:00
|
|
|
|
2008-02-18 04:37:26 +01:00
|
|
|
return c;
|
|
|
|
|
|
|
|
invalid:
|
|
|
|
|
|
|
|
sf_close( in );
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-02-17 23:03:31 +01:00
|
|
|
|
2008-04-16 10:44:31 +02:00
|
|
|
Audio_File_SF *
|
|
|
|
Audio_File_SF::create ( const char *filename, nframes_t samplerate, int channels, const char *format )
|
|
|
|
{
|
|
|
|
SF_INFO si;
|
|
|
|
SNDFILE *out;
|
|
|
|
|
|
|
|
memset( &si, 0, sizeof( si ) );
|
|
|
|
|
2008-04-26 07:33:04 +02:00
|
|
|
const Audio_File::format_desc *fd = Audio_File::find_format( Audio_File_SF::supported_formats, format );
|
|
|
|
|
|
|
|
if ( ! fd )
|
|
|
|
return (Audio_File_SF *)1;
|
|
|
|
|
2008-04-16 10:44:31 +02:00
|
|
|
si.samplerate = samplerate;
|
|
|
|
si.channels = channels;
|
2008-04-26 07:33:04 +02:00
|
|
|
si.format = fd->id;
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2008-04-26 07:33:04 +02:00
|
|
|
char *name;
|
|
|
|
asprintf( &name, "%s.%s", filename, fd->extension );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2008-04-26 07:33:04 +02:00
|
|
|
// if ( ! ( out = sf_open( name, SFM_RDWR, &si ) ) )
|
|
|
|
if ( ! ( out = sf_open( name, SFM_WRITE, &si ) ) )
|
2008-04-16 10:44:31 +02:00
|
|
|
{
|
|
|
|
printf( "couldn't create soundfile.\n" );
|
2008-04-26 07:33:04 +02:00
|
|
|
free( name );
|
2008-04-16 10:44:31 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Audio_File_SF *c = new Audio_File_SF;
|
|
|
|
|
2008-04-26 07:33:04 +02:00
|
|
|
c->_filename = name;
|
2008-04-16 10:44:31 +02:00
|
|
|
c->_length = 0;
|
|
|
|
c->_samplerate = samplerate;
|
|
|
|
c->_channels = channels;
|
|
|
|
|
|
|
|
c->_in = out;
|
|
|
|
|
2008-04-17 21:19:41 +02:00
|
|
|
/* FIXME: 256 ? */
|
2008-04-26 08:33:44 +02:00
|
|
|
c->_peak_writer = new Peak_Writer( name, 256, channels );
|
2008-04-17 21:19:41 +02:00
|
|
|
|
2008-04-16 10:44:31 +02:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2008-02-17 23:03:31 +01:00
|
|
|
bool
|
2008-02-22 09:19:20 +01:00
|
|
|
Audio_File_SF::open ( void )
|
2008-02-17 23:03:31 +01:00
|
|
|
{
|
2008-04-16 17:35:25 +02:00
|
|
|
SF_INFO si;
|
|
|
|
|
|
|
|
assert( _in == NULL );
|
2008-02-17 23:03:31 +01:00
|
|
|
|
2008-04-16 17:35:25 +02:00
|
|
|
memset( &si, 0, sizeof( si ) );
|
2008-02-22 19:40:51 +01:00
|
|
|
|
2008-04-16 17:35:25 +02:00
|
|
|
if ( ! ( _in = sf_open( _filename, SFM_READ, &si ) ) )
|
|
|
|
return false;
|
2008-02-17 23:03:31 +01:00
|
|
|
|
2008-04-16 17:35:25 +02:00
|
|
|
_current_read = 0;
|
|
|
|
_length = si.frames;
|
|
|
|
_samplerate = si.samplerate;
|
|
|
|
_channels = si.channels;
|
2008-02-17 23:03:31 +01:00
|
|
|
|
2008-04-16 17:35:25 +02:00
|
|
|
// seek( 0 );
|
2008-02-17 23:03:31 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-02-22 09:19:20 +01:00
|
|
|
Audio_File_SF::close ( void )
|
2008-02-17 23:03:31 +01:00
|
|
|
{
|
2008-04-16 17:35:25 +02:00
|
|
|
if ( _in )
|
|
|
|
sf_close( _in );
|
|
|
|
|
2008-04-17 21:19:41 +02:00
|
|
|
if ( _peak_writer )
|
|
|
|
delete _peak_writer;
|
|
|
|
|
2008-04-16 17:35:25 +02:00
|
|
|
_in = NULL;
|
2008-02-17 23:03:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-02-22 09:19:20 +01:00
|
|
|
Audio_File_SF::seek ( nframes_t offset )
|
2008-02-17 23:03:31 +01:00
|
|
|
{
|
2008-04-10 05:44:02 +02:00
|
|
|
if ( offset != _current_read )
|
|
|
|
{
|
2008-04-18 02:26:46 +02:00
|
|
|
sf_seek( _in, _current_read = offset, SEEK_SET | SFM_READ );
|
2008-04-10 05:44:02 +02:00
|
|
|
}
|
2008-02-17 23:03:31 +01:00
|
|
|
}
|
|
|
|
|
2008-03-27 00:21:11 +01:00
|
|
|
/* if channels is -1, then all channels are read into buffer
|
|
|
|
(interleaved). buf should be big enough to hold them all */
|
2008-02-17 23:03:31 +01:00
|
|
|
nframes_t
|
2008-02-22 15:03:00 +01:00
|
|
|
Audio_File_SF::read ( sample_t *buf, int channel, nframes_t len )
|
2008-02-17 23:03:31 +01:00
|
|
|
{
|
2008-04-09 04:20:44 +02:00
|
|
|
if ( len > 256 * 100 )
|
|
|
|
printf( "warning: attempt to read an insane number of frames (%lu) from soundfile\n", len );
|
|
|
|
|
|
|
|
// printf( "len = %lu, channels = %d\n", len, _channels );
|
|
|
|
|
2008-04-10 05:44:02 +02:00
|
|
|
nframes_t rlen;
|
|
|
|
|
2008-03-27 00:21:11 +01:00
|
|
|
if ( _channels == 1 || channel == -1 )
|
2008-04-10 05:44:02 +02:00
|
|
|
rlen = sf_readf_float( _in, buf, len );
|
2008-03-27 00:21:11 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
sample_t *tmp = new sample_t[ len * _channels ];
|
2008-02-22 15:03:00 +01:00
|
|
|
|
2008-04-10 05:44:02 +02:00
|
|
|
rlen = sf_readf_float( _in, tmp, len );
|
2008-02-22 15:03:00 +01:00
|
|
|
|
2008-03-27 00:21:11 +01:00
|
|
|
/* extract the requested channel */
|
2008-04-12 23:50:23 +02:00
|
|
|
for ( int i = channel; i < rlen * _channels; i += _channels )
|
2008-03-27 00:21:11 +01:00
|
|
|
*(buf++) = tmp[ i ];
|
2008-02-22 15:03:00 +01:00
|
|
|
|
2008-04-16 20:08:00 +02:00
|
|
|
delete[] tmp;
|
2008-03-27 00:21:11 +01:00
|
|
|
}
|
2008-04-10 05:44:02 +02:00
|
|
|
|
|
|
|
_current_read += rlen;
|
|
|
|
|
|
|
|
return rlen;
|
2008-02-17 23:03:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** read samples from /start/ to /end/ into /buf/ */
|
|
|
|
nframes_t
|
2008-02-22 15:03:00 +01:00
|
|
|
Audio_File_SF::read ( sample_t *buf, int channel, nframes_t start, nframes_t end )
|
2008-02-17 23:03:31 +01:00
|
|
|
{
|
2008-04-09 02:05:15 +02:00
|
|
|
assert( end > start );
|
|
|
|
|
2008-04-16 20:08:00 +02:00
|
|
|
// open();
|
2008-02-17 23:03:31 +01:00
|
|
|
|
|
|
|
seek( start );
|
|
|
|
|
2008-02-22 15:03:00 +01:00
|
|
|
nframes_t len = read( buf, channel, end - start );
|
2008-02-17 23:03:31 +01:00
|
|
|
|
2008-04-16 20:08:00 +02:00
|
|
|
// close();
|
2008-02-17 23:03:31 +01:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
2008-04-16 10:44:31 +02:00
|
|
|
|
|
|
|
/** write /nframes/ from /buf/ to soundfile. Should be interleaved for
|
|
|
|
* the appropriate number of channels */
|
|
|
|
nframes_t
|
|
|
|
Audio_File_SF::write ( sample_t *buf, nframes_t nframes )
|
|
|
|
{
|
2008-04-17 21:19:41 +02:00
|
|
|
_peak_writer->write( buf, nframes );
|
|
|
|
|
2008-04-18 02:26:46 +02:00
|
|
|
nframes_t l = sf_writef_float( _in, buf, nframes );
|
|
|
|
|
|
|
|
_length += l;
|
|
|
|
|
|
|
|
return l;
|
2008-04-16 10:44:31 +02:00
|
|
|
}
|