2008-02-17 00:12:23 +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. */
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
|
|
|
#include "Peaks.H"
|
2008-03-20 09:43:45 +01:00
|
|
|
|
|
|
|
// #include "Timeline.H"
|
2008-02-17 00:12:23 +01:00
|
|
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2008-02-17 00:28:48 +01:00
|
|
|
#include <unistd.h>
|
2008-02-17 00:12:23 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2008-02-17 03:36:17 +01:00
|
|
|
#include <sndfile.h>
|
|
|
|
|
2008-02-22 09:19:20 +01:00
|
|
|
#include "Audio_File.H"
|
2008-02-17 03:36:17 +01:00
|
|
|
|
2008-02-17 21:41:59 +01:00
|
|
|
#include "assert.h"
|
|
|
|
|
2008-02-18 04:37:26 +01:00
|
|
|
#include <math.h>
|
|
|
|
|
2008-04-12 21:13:30 +02:00
|
|
|
#include <FL/Fl.H> // for Fl::check();
|
|
|
|
|
2008-03-25 22:17:24 +01:00
|
|
|
Peaks::peakbuffer Peaks::_peakbuf;
|
2008-02-17 21:41:59 +01:00
|
|
|
|
|
|
|
|
2008-04-17 21:19:41 +02:00
|
|
|
static
|
|
|
|
const char *
|
2008-04-18 00:12:38 +02:00
|
|
|
peakname ( const char *filename, int chunksize )
|
2008-04-17 21:19:41 +02:00
|
|
|
{
|
|
|
|
static char file[512];
|
|
|
|
|
2008-04-18 00:12:38 +02:00
|
|
|
snprintf( file, 512, "%s-[%d].peak", filename, chunksize );
|
2008-04-17 21:19:41 +02:00
|
|
|
|
|
|
|
return (const char*)&file;
|
|
|
|
}
|
|
|
|
|
2008-02-17 21:41:59 +01:00
|
|
|
/** 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-04-17 17:19:04 +02:00
|
|
|
Peaks::fill_buffer ( float fpp, nframes_t s, nframes_t e ) const
|
2008-02-17 21:41:59 +01:00
|
|
|
{
|
2008-03-07 00:21:57 +01:00
|
|
|
_fpp = fpp;
|
|
|
|
|
2008-04-17 15:27:35 +02:00
|
|
|
return read_peaks( s, e, (e - s) / fpp, fpp );
|
2008-02-17 21:41:59 +01:00
|
|
|
}
|
|
|
|
|
2008-04-18 00:12:38 +02:00
|
|
|
static int
|
|
|
|
nearest_power_of_two ( int v )
|
2008-04-17 22:37:38 +02:00
|
|
|
{
|
2008-04-18 00:12:38 +02:00
|
|
|
int p = 1;
|
|
|
|
while ( 1 << p < v )
|
|
|
|
++p;
|
2008-04-17 22:37:38 +02:00
|
|
|
|
2008-04-18 00:12:38 +02:00
|
|
|
return 1 << p;
|
2008-04-17 22:37:38 +02:00
|
|
|
}
|
2008-04-18 00:12:38 +02:00
|
|
|
|
|
|
|
const int MAX_CHUNKSIZE = 4096;
|
|
|
|
const int MIN_CHUNKSIZE = 256;
|
2008-04-17 22:37:38 +02:00
|
|
|
|
2008-02-17 10:00:38 +01:00
|
|
|
int
|
2008-03-27 00:21:11 +01:00
|
|
|
Peaks::read_peakfile_peaks ( Peak *peaks, nframes_t s, int npeaks, int chunksize ) const
|
2008-02-17 04:32:31 +01:00
|
|
|
{
|
2008-03-27 01:31:57 +01:00
|
|
|
FILE *fp;
|
2008-03-27 00:21:11 +01:00
|
|
|
|
2008-04-18 00:12:38 +02:00
|
|
|
int best_fit = nearest_power_of_two( chunksize );
|
|
|
|
|
|
|
|
int pfchunksize;
|
|
|
|
|
|
|
|
// for ( pfchunksize = best_fit; pfchunksize < MAX_CHUNKSIZE; pfchunksize <<= 1 )
|
|
|
|
for ( pfchunksize = best_fit; pfchunksize >= MIN_CHUNKSIZE; pfchunksize >>= 1 )
|
|
|
|
if ( ( fp = fopen( peakname( _clip->name(), pfchunksize ), "r" ) ) )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ( ! fp )
|
2008-03-27 00:21:11 +01:00
|
|
|
{
|
2008-04-16 20:23:34 +02:00
|
|
|
printf( "failed to open peak file!\n" );
|
2008-04-18 00:12:38 +02:00
|
|
|
|
2008-03-27 00:21:11 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-03-27 01:31:57 +01:00
|
|
|
int channels = _clip->channels();
|
|
|
|
const int ratio = chunksize / pfchunksize;
|
|
|
|
|
2008-04-17 15:27:35 +02:00
|
|
|
/* locate to start position */
|
|
|
|
if ( fseek( fp, (s * channels / pfchunksize) * sizeof( Peak ), SEEK_CUR ) )
|
|
|
|
/* failed to seek... peaks not ready? */
|
|
|
|
return 0;
|
|
|
|
|
2008-03-27 01:31:57 +01:00
|
|
|
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
|
|
|
|
2008-04-16 20:08:00 +02:00
|
|
|
size_t len = 0;
|
2008-03-27 01:31:57 +01:00
|
|
|
|
2008-02-17 10:00:38 +01:00
|
|
|
int i;
|
|
|
|
for ( i = 0; i < npeaks; ++i )
|
|
|
|
{
|
|
|
|
/* read in a buffer */
|
2008-03-27 01:31:57 +01:00
|
|
|
len = fread( pbuf, sizeof( Peak ) * channels, ratio, fp );
|
|
|
|
|
|
|
|
Peak *pk = peaks + (i * channels);
|
2008-02-17 04:32:31 +01:00
|
|
|
|
2008-03-27 01:31:57 +01:00
|
|
|
/* get the peak for each channel */
|
|
|
|
for ( int j = 0; j < channels; ++j )
|
|
|
|
{
|
2008-04-17 18:46:44 +02:00
|
|
|
Peak *p = &pk[ j ];
|
2008-03-27 00:21:11 +01:00
|
|
|
|
2008-04-17 18:46:44 +02:00
|
|
|
p->min = 0;
|
|
|
|
p->max = 0;
|
2008-03-27 00:21:11 +01:00
|
|
|
|
2008-04-17 18:30:20 +02:00
|
|
|
const Peak *pb = pbuf + j;
|
|
|
|
|
2008-04-26 13:20:02 +02:00
|
|
|
for ( int k = len; k--; pb += channels )
|
2008-03-27 01:31:57 +01:00
|
|
|
{
|
2008-04-17 18:46:44 +02:00
|
|
|
if ( pb->max > p->max )
|
|
|
|
p->max = pb->max;
|
|
|
|
if ( pb->min < p->min )
|
|
|
|
p->min = pb->min;
|
2008-03-27 01:31:57 +01:00
|
|
|
}
|
2008-02-17 04:32:31 +01:00
|
|
|
|
2008-03-27 01:31:57 +01:00
|
|
|
}
|
2008-03-27 00:21:11 +01:00
|
|
|
|
2008-03-27 01:31:57 +01:00
|
|
|
if ( len < ratio )
|
2008-03-27 00:21:11 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-04-14 10:55:03 +02:00
|
|
|
delete[] pbuf;
|
2008-03-27 00:21:11 +01:00
|
|
|
|
2008-03-27 01:31:57 +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-02-17 10:00:38 +01:00
|
|
|
{
|
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 ];
|
|
|
|
}
|
|
|
|
|
2008-02-17 10:00:38 +01:00
|
|
|
}
|
2008-02-17 04:32:31 +01:00
|
|
|
|
2008-02-17 10:00:38 +01:00
|
|
|
if ( len < chunksize )
|
|
|
|
break;
|
|
|
|
}
|
2008-02-17 04:32:31 +01:00
|
|
|
|
2008-04-14 10:55:03 +02:00
|
|
|
delete[] fbuf;
|
2008-02-17 04:32:31 +01:00
|
|
|
|
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
|
|
|
|
{
|
2008-04-16 20:08:00 +02:00
|
|
|
// _clip->open();
|
2008-03-27 01:46:38 +01:00
|
|
|
_clip->seek( s );
|
|
|
|
|
|
|
|
int i = read_source_peaks( peaks, npeaks, chunksize );
|
|
|
|
|
2008-04-16 20:08:00 +02:00
|
|
|
// _clip->close();
|
2008-03-27 00:21:11 +01:00
|
|
|
|
2008-02-17 10:00:38 +01:00
|
|
|
return i;
|
|
|
|
}
|
2008-02-17 04:32:31 +01:00
|
|
|
|
2008-04-17 15:27:35 +02:00
|
|
|
int
|
2008-04-17 17:19:04 +02:00
|
|
|
Peaks::read_peaks ( nframes_t s, nframes_t e, int npeaks, int chunksize ) const
|
2008-02-17 10:00:38 +01:00
|
|
|
{
|
2008-02-18 02:13:20 +01:00
|
|
|
printf( "reading peaks %d @ %d\n", npeaks, chunksize );
|
2008-02-17 04:32:31 +01:00
|
|
|
|
2008-03-27 00:21:11 +01:00
|
|
|
if ( _peakbuf.size < npeaks * _clip->channels() )
|
2008-02-17 10:00:38 +01:00
|
|
|
{
|
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-02-17 10:00:38 +01:00
|
|
|
}
|
|
|
|
|
2008-04-17 15:27:35 +02:00
|
|
|
assert( s >= 0 );
|
|
|
|
|
2008-03-25 22:17:24 +01:00
|
|
|
_peakbuf.offset = s;
|
|
|
|
_peakbuf.buf->chunksize = chunksize;
|
2008-03-27 01:31:57 +01:00
|
|
|
|
|
|
|
/* 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 );
|
|
|
|
|
2008-04-17 15:27:35 +02:00
|
|
|
return _peakbuf.len;
|
2008-02-17 04:32:31 +01:00
|
|
|
}
|
|
|
|
|
2008-02-17 03:36:17 +01:00
|
|
|
bool
|
2008-02-18 00:33:59 +01:00
|
|
|
Peaks::open ( void )
|
2008-02-17 03:36:17 +01:00
|
|
|
{
|
2008-02-18 00:33:59 +01:00
|
|
|
const char *filename = _clip->name();
|
|
|
|
|
2008-02-17 00:12:23 +01:00
|
|
|
int fd;
|
2008-02-17 03:36:17 +01:00
|
|
|
|
2008-04-17 17:59:02 +02:00
|
|
|
if ( ! current() )
|
2008-04-17 21:19:41 +02:00
|
|
|
/* Build peaks asyncronously */
|
2008-04-17 17:59:02 +02:00
|
|
|
if ( ! fork() )
|
|
|
|
exit( make_peaks( 256 ) );
|
2008-02-17 03:59:45 +01:00
|
|
|
|
2008-04-18 00:12:38 +02:00
|
|
|
/* FIXME: 256 == bogus */
|
|
|
|
if ( ( fd = ::open( peakname( filename, 256 ), O_RDONLY ) ) < 0 )
|
2008-02-17 03:59:45 +01:00
|
|
|
return false;
|
2008-02-17 00:12:23 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
struct stat st;
|
2008-02-17 03:59:45 +01:00
|
|
|
|
2008-02-17 00:12:23 +01:00
|
|
|
fstat( fd, &st );
|
2008-02-17 03:59:45 +01:00
|
|
|
|
2008-02-17 00:12:23 +01:00
|
|
|
_len = st.st_size;
|
|
|
|
}
|
|
|
|
|
2008-02-17 00:28:48 +01:00
|
|
|
::close( fd );
|
|
|
|
|
2008-02-17 00:12:23 +01:00
|
|
|
_len = (_len - sizeof( int )) / sizeof( Peak );
|
2008-02-17 00:28:48 +01:00
|
|
|
|
|
|
|
return true;
|
2008-02-17 00:12:23 +01:00
|
|
|
}
|
2008-02-17 03:36:17 +01:00
|
|
|
|
2008-02-17 03:59:45 +01:00
|
|
|
/** returns false if peak file for /filename/ is out of date */
|
2008-02-17 03:36:17 +01:00
|
|
|
bool
|
2008-02-18 00:33:59 +01:00
|
|
|
Peaks::current ( void ) const
|
2008-02-17 03:36:17 +01:00
|
|
|
{
|
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-04-18 00:12:38 +02:00
|
|
|
/* FIXME: 256 == bogus */
|
|
|
|
if ( ( pfd = ::open( peakname( _clip->name(), 256 ), O_RDONLY ) ) < 0 )
|
2008-03-27 00:21:11 +01:00
|
|
|
return false;
|
2008-02-17 03:36:17 +01:00
|
|
|
|
2008-02-17 03:59:45 +01:00
|
|
|
struct stat sst, pst;
|
2008-02-17 03:36:17 +01:00
|
|
|
|
2008-02-17 03:59:45 +01:00
|
|
|
fstat( sfd, &sst );
|
|
|
|
fstat( pfd, &pst );
|
2008-02-17 03:36:17 +01:00
|
|
|
|
2008-02-17 03:59:45 +01:00
|
|
|
close( sfd );
|
|
|
|
close( pfd );
|
|
|
|
|
|
|
|
return sst.st_mtime <= pst.st_mtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-12 21:13:30 +02:00
|
|
|
/* FIXME: we need to work out a way to run this in another thread and
|
2008-04-17 21:19:41 +02:00
|
|
|
possibly stream back the data to the GUI */
|
2008-02-17 03:59:45 +01:00
|
|
|
/** 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;
|
|
|
|
|
2008-04-16 20:08:00 +02:00
|
|
|
_clip->seek( 0 );
|
|
|
|
|
2008-04-18 00:12:38 +02:00
|
|
|
FILE *fp = fopen( peakname( filename, chunksize ), "w" );
|
2008-02-17 03:36:17 +01:00
|
|
|
|
2008-04-18 00:12:38 +02:00
|
|
|
if ( ! fp )
|
2008-02-17 03:36:17 +01:00
|
|
|
return false;
|
|
|
|
|
2008-03-27 00:21:11 +01:00
|
|
|
Peak peaks[ _clip->channels() ];
|
|
|
|
|
2008-02-17 03:36:17 +01:00
|
|
|
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 );
|
2008-02-17 03:36:17 +01:00
|
|
|
}
|
2008-02-18 00:29:09 +01:00
|
|
|
while ( len );
|
2008-02-17 03:36:17 +01:00
|
|
|
|
2008-02-18 00:29:09 +01:00
|
|
|
fclose( fp );
|
2008-02-18 00:33:59 +01:00
|
|
|
|
|
|
|
return true;
|
2008-02-17 03:36:17 +01:00
|
|
|
}
|
2008-02-18 04:37:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
/** return normalization factor for range of samples from /start/ to
|
|
|
|
/end/ (uses known peak data if possible */
|
|
|
|
|
2008-05-01 09:13:58 +02:00
|
|
|
float
|
|
|
|
//Peaks::normalization_factor( float fpp, nframes_t start, nframes_t end ) const
|
|
|
|
Peak::normalization_factor( void ) const
|
|
|
|
{
|
|
|
|
float s;
|
2008-02-18 04:37:26 +01:00
|
|
|
|
2008-05-01 09:13:58 +02:00
|
|
|
s = 1.0f / fabs( this->max );
|
2008-02-18 04:37:26 +01:00
|
|
|
|
2008-05-01 09:13:58 +02:00
|
|
|
if ( s * this->min < -1.0 )
|
|
|
|
s = 1.0f / fabs( this->min );
|
2008-02-18 04:37:26 +01:00
|
|
|
|
2008-05-01 09:13:58 +02:00
|
|
|
return s;
|
|
|
|
}
|
2008-04-17 21:19:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Peak_Writer::Peak_Writer ( const char *filename, int chunksize, int channels )
|
|
|
|
{
|
|
|
|
|
2008-04-17 22:37:38 +02:00
|
|
|
_channels = channels;
|
2008-04-17 21:19:41 +02:00
|
|
|
_chunksize = chunksize;
|
2008-04-17 22:37:38 +02:00
|
|
|
_index = 0;
|
2008-04-17 21:19:41 +02:00
|
|
|
|
|
|
|
_peak = new Peak[ channels ];
|
|
|
|
memset( _peak, 0, sizeof( Peak ) * channels );
|
|
|
|
|
2008-04-18 00:12:38 +02:00
|
|
|
if ( ! ( _fp = fopen( peakname( filename, chunksize ), "w" ) ) )
|
2008-04-17 21:19:41 +02:00
|
|
|
/* error! */;
|
|
|
|
}
|
|
|
|
|
|
|
|
Peak_Writer::~Peak_Writer ( )
|
|
|
|
{
|
|
|
|
fclose( _fp );
|
|
|
|
delete _peak;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** append peaks for samples in /buf/ to peakfile */
|
|
|
|
void
|
|
|
|
Peak_Writer::write ( sample_t *buf, nframes_t nframes )
|
|
|
|
{
|
2008-04-17 22:37:38 +02:00
|
|
|
for ( ; nframes--; ++_index, buf += _channels )
|
2008-04-17 21:19:41 +02:00
|
|
|
{
|
|
|
|
for ( int j = 0; j < _channels; ++j )
|
|
|
|
{
|
|
|
|
Peak *p = _peak + j;
|
|
|
|
|
|
|
|
if ( *buf > p->max )
|
|
|
|
p->max = *buf;
|
|
|
|
if ( *buf < p->min )
|
|
|
|
p->min = *buf;
|
|
|
|
}
|
|
|
|
|
2008-04-17 22:37:38 +02:00
|
|
|
if ( _index == _chunksize - 1 )
|
2008-04-17 21:19:41 +02:00
|
|
|
{
|
|
|
|
fwrite( _peak, sizeof( Peak ), _channels, _fp );
|
|
|
|
memset( _peak, 0, sizeof( Peak ) * _channels );
|
2008-04-17 22:37:38 +02:00
|
|
|
_index = 0;
|
2008-04-17 21:19:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|