diff --git a/Audio_File.C b/Audio_File.C index 6fecbf1..f4eada4 100644 --- a/Audio_File.C +++ b/Audio_File.C @@ -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; diff --git a/Audio_File.H b/Audio_File.H index 31616cd..e7670c5 100644 --- a/Audio_File.H +++ b/Audio_File.H @@ -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; }; diff --git a/Audio_File_SF.C b/Audio_File_SF.C index c1b4c65..186795c 100644 --- a/Audio_File_SF.C +++ b/Audio_File_SF.C @@ -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(); diff --git a/Audio_File_SF.H b/Audio_File_SF.H index 8b74648..d43375e 100644 --- a/Audio_File_SF.H +++ b/Audio_File_SF.H @@ -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 ); }; diff --git a/Peaks.C b/Peaks.C index 211dfe0..faf4199 100644 --- a/Peaks.C +++ b/Peaks.C @@ -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;