Handle peaks for multichannel files.

This commit is contained in:
Jonathan Moore Liles 2008-02-22 12:40:51 -06:00
parent 6cfe9e8b6c
commit f8fb113886
8 changed files with 61 additions and 27 deletions

View File

@ -36,6 +36,14 @@ Audio_File::from_file ( const char * filename )
done:
a->_peaks.open();
a->_peaks = new Peaks[ a->channels() ];
for ( int i = a->channels(); i-- ; )
{
a->_peaks[i].channel( i );
a->_peaks[i].clip( a );
a->_peaks[i].open();
}
return a;
}

View File

@ -32,22 +32,24 @@ class Audio_File
protected:
Peaks _peaks;
const char *_filename;
nframes_t _length; /* length of file in samples */
int _channels;
Peaks *_peaks;
public:
Audio_File ( ) : _peaks( this )
Audio_File ( )
{
_filename = NULL;
_length = 0;
_length = _channels = 0;
}
static Audio_File *from_file ( const char *filename );
Peaks const * peaks ( void ) { return &_peaks; }
Peaks const * peaks ( int channel ) { return &_peaks[ channel ]; }
const char *name ( void ) const { return _filename; }
nframes_t length ( void ) const { return _length; }
int channels ( void ) const { return _channels; }

View File

@ -59,7 +59,8 @@ Audio_File_SF::from_file ( const char *filename )
c->_length = si.frames;
c->_channels = si.channels;
sf_close( in );
c->_in = in;
// sf_close( in );
return c;
@ -72,12 +73,13 @@ invalid:
bool
Audio_File_SF::open ( void )
{
SF_INFO si;
memset( &si, 0, sizeof( si ) );
/* SF_INFO si; */
if ( ! ( _in = sf_open( _filename, SFM_READ, &si ) ) )
return false;
/* memset( &si, 0, sizeof( si ) ); */
/* if ( ! ( _in = sf_open( _filename, SFM_READ, &si ) ) ) */
/* return false; */
return true;
}
@ -85,7 +87,7 @@ Audio_File_SF::open ( void )
void
Audio_File_SF::close ( void )
{
sf_close( _in );
// sf_close( _in );
}
void

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, 0, chunksize );
len = _clip->read( fbuf, _channel, chunksize );
Peak &p = peaks[ i ];
p.min = 0;
@ -178,13 +178,13 @@ Peaks::operator[] ( int X ) const
return peak( timeline->x_to_ts( X ), timeline->x_to_ts( X + 1 ) );
}
static
const char *
peakname ( const char *filename )
Peaks::peakname ( const char *filename ) const
{
static char file[512];
snprintf( file, 512, "%s.peak", filename );
snprintf( file, 512, "%s.peak-%d", filename, _channel );
return (const char*)&file;
}

20
Peaks.H
View File

@ -57,6 +57,7 @@ class Peaks
static peakbuffer peakbuf;
Audio_File *_clip;
int _channel;
peakdata *_peaks;
@ -65,12 +66,25 @@ class Peaks
void read_peaks ( int s, int e, int npeaks, int chunksize ) const;
int clip_read_peaks ( Peak *peaks, int npeaks, int chunksize ) const;
const char *peakname ( const char *filename ) const;
Peak & peak ( nframes_t start, nframes_t end ) const;
Peaks ( );
// Peaks ( );
public:
Peaks ( )
{
_peaks = new peakdata;
_peaks->chunksize = 0;
_len = 0;
_clip = NULL;
_channel = 0;
}
Peaks ( Audio_File *c )
{
_peaks = new peakdata;
@ -78,8 +92,12 @@ public:
_peaks->chunksize = 0;
_len = 0;
_clip = c;
_channel = 0;
}
void clip ( Audio_File *c ) { _clip = c; }
void channel ( int v ) { _channel = v; }
void fill_buffer ( int s, int e ) const;
void downsample ( Peak *peaks, int s, int e, float *mhi, float *mlo ) const;

View File

@ -345,7 +345,11 @@ Region::draw ( int X, int Y, int W, int H )
// fl_push_clip( x() + Fl::box_dx( box() ), y(), w() - Fl::box_dw( box() ), h() );
draw_waveform( rx, Y, rw, H, _clip, _start + offset, min( (_end - _start) - offset, _end), _scale, _selected ? _color : fl_invert_color( _color ) );
int ch = H / _clip->channels();
for ( int i = _clip->channels(); i--; )
draw_waveform( rx, Y + (i * ch), rw, ch, _clip, i,
_start + offset, min( (_end - _start) - offset, _end),
_scale, _selected ? _color : fl_invert_color( _color ) );
timeline->draw_measure_lines( rx, Y, rw, H, _box_color );
@ -376,7 +380,8 @@ Region::normalize ( void )
{
printf( "normalize: start=%lu end=%lu\n", _start, _end );
_scale = _clip->peaks()->normalization_factor( _start, _end );
/* FIXME: punt */
_scale = _clip->peaks( 0 )->normalization_factor( _start, _end );
}

View File

@ -31,7 +31,7 @@
/** draw a portion of /clip/'s waveform. coordinates are the portion to draw */
void
draw_waveform ( int X, int Y, int W, int H, Audio_File *_clip, nframes_t _start, nframes_t _end, float _scale, Fl_Color color )
draw_waveform ( int X, int Y, int W, int H, Audio_File *_clip, int channel, nframes_t _start, nframes_t _end, float _scale, Fl_Color color )
{
fl_push_clip( X, Y, W, H );
@ -39,15 +39,14 @@ draw_waveform ( int X, int Y, int W, int H, Audio_File *_clip, nframes_t _start,
int start = timeline->ts_to_x( _start );
{
_clip->peaks()->fill_buffer( _start,
_start + timeline->x_to_ts( W ) );
}
const Peaks *pk = _clip->peaks( channel );
pk->fill_buffer( _start, _start + timeline->x_to_ts( W ) );
j = start;
for ( int x = X; x < X + W; ++x, ++j )
{
Peak p = (*_clip->peaks())[ j ];
Peak p = (*pk)[ j ];
int mid = Y + (H / 2);
@ -73,7 +72,7 @@ draw_waveform ( int X, int Y, int W, int H, Audio_File *_clip, nframes_t _start,
j = start;
for ( int x = X; x < X + W; ++x, ++j )
{
Peak p = (*_clip->peaks())[ j ];
Peak p = (*pk)[ j ];
p.min *= _scale;
@ -87,7 +86,7 @@ draw_waveform ( int X, int Y, int W, int H, Audio_File *_clip, nframes_t _start,
j = start;
for ( int x = X; x < X + W; ++x, ++j )
{
Peak p = (*_clip->peaks())[ j ];
Peak p = (*pk)[ j ];
p.max *= _scale;

View File

@ -26,4 +26,4 @@
#include "Audio_File.H"
void draw_waveform ( int X, int Y, int W, int H, Audio_File *_clip, nframes_t _start, nframes_t _end, float _scale, Fl_Color color );
void draw_waveform ( int X, int Y, int W, int H, Audio_File *_clip, int channel, nframes_t _start, nframes_t _end, float _scale, Fl_Color color );