Support multichannel sound files.

This commit is contained in:
Jonathan Moore Liles 2008-02-22 08:03:00 -06:00
parent 0e6352c54a
commit 6cfe9e8b6c
5 changed files with 31 additions and 18 deletions

View File

@ -30,8 +30,6 @@ Audio_File::from_file ( const char * filename )
if ( ( a = Audio_File_SF::from_file( filename ) ) )
goto done;
a->_peaks.open();
// TODO: other formats
return NULL;

View File

@ -35,6 +35,7 @@ protected:
Peaks _peaks;
const char *_filename;
nframes_t _length; /* length of file in samples */
int _channels;
public:
@ -47,15 +48,16 @@ public:
static Audio_File *from_file ( const char *filename );
Peaks const * peaks ( void ) { return &_peaks; }
const char *name ( void ) { return _filename; }
nframes_t length ( void ) { return _length; }
const char *name ( void ) const { return _filename; }
nframes_t length ( void ) const { return _length; }
int channels ( void ) const { return _channels; }
// Peaks const * peaks ( void ) { return &_peaks; }
virtual bool open ( void ) = 0;
virtual void close ( void ) = 0;
virtual void seek ( nframes_t offset ) = 0;
virtual nframes_t read ( sample_t *buf, nframes_t len ) = 0;
virtual nframes_t read ( sample_t *buf, nframes_t start, nframes_t end ) = 0;
virtual nframes_t read ( sample_t *buf, int channel, nframes_t len ) = 0;
virtual nframes_t read ( sample_t *buf, int channel, nframes_t start, nframes_t end ) = 0;
};

View File

@ -41,11 +41,11 @@ Audio_File_SF::from_file ( const char *filename )
return NULL;
}
if ( si.channels != 1 )
{
printf( "error: incompatible format\n" );
goto invalid;
}
/* if ( si.channels != 1 ) */
/* { */
/* printf( "error: incompatible format\n" ); */
/* goto invalid; */
/* } */
if ( si.samplerate != timeline->sample_rate )
{
@ -57,6 +57,7 @@ Audio_File_SF::from_file ( const char *filename )
c->_filename = filename;
c->_length = si.frames;
c->_channels = si.channels;
sf_close( in );
@ -94,20 +95,32 @@ Audio_File_SF::seek ( nframes_t offset )
}
nframes_t
Audio_File_SF::read ( sample_t *buf, nframes_t len )
Audio_File_SF::read ( sample_t *buf, int channel, nframes_t len )
{
return sf_read_float ( _in, buf, len );
if ( _channels == 1 )
return sf_read_float ( _in, buf, len );
sample_t *tmp = new sample_t[ len * _channels ];
nframes_t rlen = sf_readf_float( _in, tmp, len );
for ( int i = channel; i < rlen; i += _channels )
*(buf++) = tmp[ i ];
delete tmp;
return rlen;
}
/** read samples from /start/ to /end/ into /buf/ */
nframes_t
Audio_File_SF::read ( sample_t *buf, nframes_t start, nframes_t end )
Audio_File_SF::read ( sample_t *buf, int channel, nframes_t start, nframes_t end )
{
open();
seek( start );
nframes_t len = read( buf, end - start );
nframes_t len = read( buf, channel, end - start );
close();

View File

@ -34,7 +34,7 @@ public:
bool open ( void );
void close ( void );
void seek ( nframes_t offset );
nframes_t read ( sample_t *buf, nframes_t len );
nframes_t read ( sample_t *buf, nframes_t start, nframes_t end );
nframes_t read ( sample_t *buf, int channel, nframes_t len );
nframes_t read ( sample_t *buf, int channel, nframes_t start, nframes_t end );
};

View File

@ -92,7 +92,7 @@ Peaks::clip_read_peaks ( Peak *peaks, int npeaks, int chunksize ) const
for ( i = 0; i < npeaks; ++i )
{
/* read in a buffer */
len = _clip->read( fbuf, chunksize );
len = _clip->read( fbuf, 0, chunksize );
Peak &p = peaks[ i ];
p.min = 0;