non/Timeline/Peaks.C

372 lines
8.7 KiB
C++
Raw Normal View History

/*******************************************************************************/
/* 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. */
/*******************************************************************************/
#include "Peaks.H"
2008-03-20 09:43:45 +01:00
// #include "Timeline.H"
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
2008-02-17 00:28:48 +01:00
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sndfile.h>
#include "Audio_File.H"
#include "assert.h"
#include <math.h>
2008-03-25 22:17:24 +01:00
Peaks::peakbuffer Peaks::_peakbuf;
/** Prepare a buffer of peaks from /s/ to /e/ for reading. Must be
* called before any calls to operator[] */
2008-03-25 22:17:24 +01:00
int
2008-03-07 00:21:57 +01:00
Peaks::fill_buffer ( float fpp, int s, int e ) const
{
2008-03-07 00:21:57 +01:00
_fpp = fpp;
read_peaks( s, e, (e - s) / fpp, fpp );
2008-03-25 22:17:24 +01:00
/* FIXME: are we *SURE* we got them all? */
return (e - s) / fpp;
}
2008-03-27 03:56:11 +01:00
/* inline void */
/* Peaks::downsample ( Peak *peaks, int s, int e, float *mhi, float *mlo ) const */
/* { */
/* *mhi = 0; */
/* *mlo = 0; */
/* if ( e > _len ) */
/* e = _len; */
/* for ( int j = s; j < e; j++ ) */
/* { */
/* const float lo = peaks[ j ].min; */
/* const float hi = peaks[ j ].max; */
/* if ( hi > *mhi ) */
/* *mhi = hi; */
/* if ( lo < *mlo ) */
/* *mlo = lo; */
/* } */
/* } */
int
2008-03-27 00:21:11 +01:00
Peaks::read_peakfile_peaks ( Peak *peaks, nframes_t s, int npeaks, int chunksize ) const
{
FILE *fp;
2008-03-27 00:21:11 +01:00
if ( ! ( fp = fopen( peakname( _clip->name() ), "r" ) ) )
2008-03-27 00:21:11 +01:00
{
printf( "failed to open peak file!" );
return 0;
}
/* get chunk size of peak file */
int pfchunksize;
fread( &pfchunksize, sizeof( int ), 1, fp );
int channels = _clip->channels();
const int ratio = chunksize / pfchunksize;
if ( ratio == 1 )
{
int len = fread( peaks, sizeof( Peak ) * channels, npeaks, fp );
fclose( fp );
return len;
}
Peak *pbuf = new Peak[ ratio * channels ];
2008-03-27 00:21:11 +01:00
/* locate to start position */
fseek( fp, (s * channels / pfchunksize) * sizeof( Peak ), SEEK_CUR );
size_t len;
int i;
for ( i = 0; i < npeaks; ++i )
{
/* read in a buffer */
len = fread( pbuf, sizeof( Peak ) * channels, ratio, fp );
Peak *pk = peaks + (i * channels);
/* get the peak for each channel */
for ( int j = 0; j < channels; ++j )
{
Peak &p = pk[ j ];
2008-03-27 00:21:11 +01:00
p.min = 0;
p.max = 0;
2008-03-27 00:21:11 +01:00
for ( int k = j; k < len * channels; k += channels )
{
if ( pbuf[ k ].max > p.max )
p.max = pbuf[ k ].max;
if ( pbuf[ k ].min < p.min )
p.min = pbuf[ k ].min;
}
}
2008-03-27 00:21:11 +01:00
if ( len < ratio )
2008-03-27 00:21:11 +01:00
break;
}
delete pbuf;
2008-03-27 00:21:11 +01:00
fclose( fp );
2008-03-27 00:21:11 +01:00
return i;
}
int
2008-03-27 01:46:38 +01:00
Peaks::read_source_peaks ( Peak *peaks, int npeaks, int chunksize ) const
2008-03-27 00:21:11 +01:00
{
int channels = _clip->channels();
sample_t *fbuf = new sample_t[ chunksize * channels ];
size_t len;
int i;
for ( i = 0; i < npeaks; ++i )
{
/* read in a buffer */
len = _clip->read( fbuf, -1, chunksize );
Peak *pk = peaks + (i * channels);
/* get the peak for each channel */
for ( int j = 0; j < channels; ++j )
{
2008-03-27 00:21:11 +01:00
Peak &p = pk[ j ];
p.min = 0;
p.max = 0;
for ( int k = j; k < len * channels; k += channels )
{
if ( fbuf[ k ] > p.max )
p.max = fbuf[ k ];
if ( fbuf[ k ] < p.min )
p.min = fbuf[ k ];
}
}
if ( len < chunksize )
break;
}
delete fbuf;
2008-03-27 01:46:38 +01:00
return i;
}
int
Peaks::read_source_peaks ( Peak *peaks, nframes_t s, int npeaks, int chunksize ) const
{
_clip->open();
_clip->seek( s );
int i = read_source_peaks( peaks, npeaks, chunksize );
2008-03-27 00:21:11 +01:00
_clip->close();
return i;
}
void
Peaks::read_peaks ( int s, int e, int npeaks, int chunksize ) const
{
2008-02-18 02:13:20 +01:00
printf( "reading peaks %d @ %d\n", npeaks, chunksize );
2008-03-27 00:21:11 +01:00
if ( _peakbuf.size < npeaks * _clip->channels() )
{
2008-03-27 00:21:11 +01:00
_peakbuf.size = npeaks * _clip->channels();
2008-03-25 22:17:24 +01:00
// printf( "reallocating peak buffer %li\n", _peakbuf.size );
_peakbuf.buf = (peakdata*)realloc( _peakbuf.buf, sizeof( peakdata ) + (_peakbuf.size * sizeof( Peak )) );
}
2008-03-25 22:17:24 +01:00
_peakbuf.offset = s;
_peakbuf.buf->chunksize = chunksize;
/* FIXME: compart to (minimum) peakfile chunk size */
if ( chunksize < 256 )
_peakbuf.len = read_source_peaks( _peakbuf.buf->data, s, npeaks, chunksize );
else
_peakbuf.len = read_peakfile_peaks( _peakbuf.buf->data, s, npeaks, chunksize );
}
/** Return the peak for the range of samples */
2008-03-25 22:17:24 +01:00
/* Peak & */
2008-03-27 03:56:11 +01:00
/* Peaks::peak ( nframes_t start, nframes_t end ) const */
2008-03-25 22:17:24 +01:00
/* { */
2008-03-27 03:56:11 +01:00
/* /\* Is there a better way to return this? *\/ */
/* static Peak p; */
2008-03-20 09:43:45 +01:00
2008-03-27 03:56:11 +01:00
/* start = (start - _peakbuf.offset) / _peakbuf.buf->chunksize; */
/* end = (end - _peakbuf.offset) / _peakbuf.buf->chunksize; */
2008-03-25 22:17:24 +01:00
2008-03-27 03:56:11 +01:00
/* if ( end > _peakbuf.len ) */
/* end = _peakbuf.len; */
2008-03-27 03:56:11 +01:00
/* downsample( _peakbuf.buf->data, start, end, &p.max, &p.min ); */
/* return p; */
/* } */
2008-02-22 19:40:51 +01:00
const char *
2008-02-22 19:40:51 +01:00
Peaks::peakname ( const char *filename ) const
{
static char file[512];
2008-03-27 00:21:11 +01:00
snprintf( file, 512, "%s.peak", filename );
return (const char*)&file;
}
bool
2008-02-18 00:33:59 +01:00
Peaks::open ( void )
{
2008-02-18 00:33:59 +01:00
const char *filename = _clip->name();
int fd;
2008-02-18 00:33:59 +01:00
make_peaks( 256 );
2008-02-17 03:59:45 +01:00
if ( ( fd = ::open( peakname( filename ), O_RDONLY ) ) < 0 )
2008-02-17 03:59:45 +01:00
return false;
{
struct stat st;
2008-02-17 03:59:45 +01:00
fstat( fd, &st );
2008-02-17 03:59:45 +01:00
_len = st.st_size;
}
2008-02-17 00:28:48 +01:00
::close( fd );
_len = (_len - sizeof( int )) / sizeof( Peak );
2008-02-17 00:28:48 +01:00
return true;
}
2008-02-17 03:59:45 +01:00
/** returns false if peak file for /filename/ is out of date */
bool
2008-02-18 00:33:59 +01:00
Peaks::current ( void ) const
{
2008-02-17 03:59:45 +01:00
int sfd, pfd;
2008-02-18 00:33:59 +01:00
if ( ( sfd = ::open( _clip->name(), O_RDONLY ) ) < 0 )
2008-02-17 03:59:45 +01:00
return true;
2008-02-18 00:33:59 +01:00
if ( ( pfd = ::open( peakname( _clip->name() ), O_RDONLY ) ) < 0 )
2008-03-27 00:21:11 +01:00
return false;
2008-02-17 03:59:45 +01:00
struct stat sst, pst;
2008-02-17 03:59:45 +01:00
fstat( sfd, &sst );
fstat( pfd, &pst );
2008-02-17 03:59:45 +01:00
close( sfd );
close( pfd );
return sst.st_mtime <= pst.st_mtime;
}
/** build peaks file for /filename/ if necessary */
bool
2008-02-18 00:33:59 +01:00
Peaks::make_peaks ( int chunksize )
2008-02-17 03:59:45 +01:00
{
2008-02-18 00:33:59 +01:00
const char *filename = _clip->name();
if ( current() )
2008-02-17 03:59:45 +01:00
return true;
if ( ! _clip->open() )
return false;
FILE *fp = fopen( peakname( filename ), "w" );
if ( fp == NULL )
{
_clip->close();
return false;
}
/* write chunksize first */
fwrite( &chunksize, sizeof( int ), 1, fp );
2008-03-27 00:21:11 +01:00
Peak peaks[ _clip->channels() ];
size_t len;
do {
2008-03-27 01:46:38 +01:00
len = read_source_peaks( peaks, 1, chunksize );
2008-03-27 00:21:11 +01:00
fwrite( peaks, sizeof( peaks ), 1, fp );
}
while ( len );
_clip->close();
fclose( fp );
2008-02-18 00:33:59 +01:00
return true;
}
/** return normalization factor for range of samples from /start/ to
/end/ (uses known peak data if possible */
2008-03-27 03:56:11 +01:00
/* float */
/* Peaks::normalization_factor( float fpp, nframes_t start, nframes_t end ) const */
/* { */
/* float s; */
2008-03-27 03:56:11 +01:00
/* // fill_buffer( fpp, start, end ); */
2008-03-27 03:56:11 +01:00
/* /\* if ( end - start < _peaks->chunksize * 4 ) *\/ */
/* /\* fill_buffer( _clip->length() / 4, start, end ); *\/ */
/* /\* else *\/ */
/* /\* fill_buffer( _clip->length(), start, end ); *\/ */
2008-03-27 03:56:11 +01:00
/* Peak p = peak( start, end ); */
2008-03-27 03:56:11 +01:00
/* s = 1.0f / fabs( p.max ); */
2008-03-27 03:56:11 +01:00
/* if ( s * p.min < -1.0 ) */
/* s = 1.0f / fabs( p.min ); */
2008-03-27 03:56:11 +01:00
/* return s; */
/* } */