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"
|
|
|
|
#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>
|
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-17 04:32:31 +01:00
|
|
|
#include "Clip.H"
|
2008-02-17 03:36:17 +01:00
|
|
|
|
2008-02-17 21:41:59 +01:00
|
|
|
#include "assert.h"
|
|
|
|
|
|
|
|
Peaks::peakbuffer Peaks::peakbuf;
|
|
|
|
|
|
|
|
|
|
|
|
/** Prepare a buffer of peaks from /s/ to /e/ for reading. Must be
|
|
|
|
* called before any calls to operator[] */
|
|
|
|
void
|
|
|
|
Peaks::fill_buffer ( int s, int e ) const
|
|
|
|
{
|
|
|
|
if ( timeline.fpp < _peaks->chunksize )
|
|
|
|
{
|
|
|
|
/* looks like we're going to have to switch to a higher resolution peak file
|
|
|
|
or read directly from the source */
|
2008-02-18 02:13:20 +01:00
|
|
|
read_peaks( s, e, (e - s) / timeline.fpp, timeline.fpp );
|
2008-02-17 21:41:59 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* we'll just downsample on the fly in this case--no need for extra copying into
|
|
|
|
the buffer */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-17 00:12:23 +01:00
|
|
|
void
|
|
|
|
Peaks::downsample ( int s, int e, float *mhi, float *mlo ) const
|
|
|
|
{
|
2008-02-17 21:41:59 +01:00
|
|
|
*mhi = 0;
|
|
|
|
*mlo = 0;
|
2008-02-17 00:12:23 +01:00
|
|
|
|
|
|
|
if ( e > _len )
|
2008-02-17 03:59:45 +01:00
|
|
|
e = _len;
|
2008-02-17 00:12:23 +01:00
|
|
|
|
|
|
|
for ( int j = s; j < e; j++ )
|
|
|
|
{
|
|
|
|
const float lo = _peaks->data[ j ].min;
|
|
|
|
const float hi = _peaks->data[ j ].max;
|
|
|
|
|
|
|
|
if ( hi > *mhi )
|
|
|
|
*mhi = hi;
|
|
|
|
if ( lo < *mlo )
|
|
|
|
*mlo = lo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-17 10:00:38 +01:00
|
|
|
int
|
2008-02-18 00:17:38 +01:00
|
|
|
Peaks::clip_read_peaks ( Peak *peaks, int npeaks, int chunksize ) const
|
2008-02-17 04:32:31 +01:00
|
|
|
{
|
2008-02-18 02:13:20 +01:00
|
|
|
sample_t *fbuf = new sample_t[ chunksize ];
|
2008-02-17 04:32:31 +01:00
|
|
|
|
2008-02-17 10:00:38 +01:00
|
|
|
size_t len;
|
2008-02-17 04:32:31 +01:00
|
|
|
|
2008-02-17 10:00:38 +01:00
|
|
|
int i;
|
|
|
|
for ( i = 0; i < npeaks; ++i )
|
|
|
|
{
|
|
|
|
/* read in a buffer */
|
2008-02-18 00:17:38 +01:00
|
|
|
len = _clip->read( fbuf, chunksize );
|
2008-02-17 04:32:31 +01:00
|
|
|
|
2008-02-17 21:41:59 +01:00
|
|
|
Peak &p = peaks[ i ];
|
|
|
|
p.min = 0;
|
|
|
|
p.max = 0;
|
2008-02-17 04:32:31 +01:00
|
|
|
|
2008-02-17 10:00:38 +01:00
|
|
|
for ( int j = len; j--; )
|
|
|
|
{
|
2008-02-17 21:41:59 +01:00
|
|
|
if ( fbuf[j] > p.max )
|
|
|
|
p.max = fbuf[j];
|
|
|
|
if ( fbuf[j] < p.min )
|
|
|
|
p.min = fbuf[j];
|
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-02-17 10:00:38 +01:00
|
|
|
delete fbuf;
|
2008-02-17 04:32:31 +01:00
|
|
|
|
2008-02-17 10:00:38 +01:00
|
|
|
return i;
|
|
|
|
}
|
2008-02-17 04:32:31 +01:00
|
|
|
|
2008-02-17 10:00:38 +01:00
|
|
|
void
|
2008-02-17 21:41:59 +01:00
|
|
|
Peaks::read_peaks ( int s, int 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-02-17 21:41:59 +01:00
|
|
|
if ( peakbuf.size < npeaks )
|
2008-02-17 10:00:38 +01:00
|
|
|
{
|
2008-02-17 21:41:59 +01:00
|
|
|
peakbuf.size = npeaks;
|
|
|
|
// 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-02-18 00:17:38 +01:00
|
|
|
_clip->open();
|
|
|
|
_clip->seek( s );
|
2008-02-17 10:00:38 +01:00
|
|
|
|
2008-02-17 21:41:59 +01:00
|
|
|
peakbuf.offset = s;
|
|
|
|
peakbuf.buf->chunksize = chunksize;
|
2008-02-18 00:17:38 +01:00
|
|
|
peakbuf.len = clip_read_peaks( peakbuf.buf->data, npeaks, chunksize );
|
2008-02-17 10:00:38 +01:00
|
|
|
|
2008-02-18 00:17:38 +01:00
|
|
|
_clip->close();
|
2008-02-17 04:32:31 +01:00
|
|
|
}
|
|
|
|
|
2008-02-17 00:12:23 +01:00
|
|
|
|
|
|
|
/* virtual array. Index is a Pixel value, and it returns the
|
|
|
|
* (resampled) peaks for that pixel based on the current timeline
|
|
|
|
* zoom. */
|
|
|
|
Peak &
|
|
|
|
Peaks::operator[] ( int X ) const
|
|
|
|
{
|
|
|
|
/* Is there a better way to return this? */
|
|
|
|
static Peak p;
|
|
|
|
|
2008-02-17 01:35:53 +01:00
|
|
|
if ( timeline.fpp < _peaks->chunksize )
|
|
|
|
{
|
2008-02-17 21:41:59 +01:00
|
|
|
assert( timeline.fpp == peakbuf.buf->chunksize );
|
|
|
|
|
|
|
|
int start = timeline.x_to_ts( X ) / peakbuf.buf->chunksize;
|
|
|
|
int i = start - (peakbuf.offset / peakbuf.buf->chunksize);
|
2008-02-17 01:35:53 +01:00
|
|
|
|
2008-02-17 21:41:59 +01:00
|
|
|
assert( peakbuf.len > i );
|
|
|
|
|
|
|
|
p = peakbuf.buf->data[ i ];
|
2008-02-17 04:32:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int start = timeline.x_to_ts( X ) / _peaks->chunksize;
|
|
|
|
int end = timeline.x_to_ts( X + 1 ) / _peaks->chunksize;
|
2008-02-17 00:12:23 +01:00
|
|
|
|
2008-02-17 04:32:31 +01:00
|
|
|
downsample( start, end, &p.max, &p.min );
|
|
|
|
}
|
2008-02-17 00:12:23 +01:00
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2008-02-17 03:36:17 +01:00
|
|
|
static
|
|
|
|
const char *
|
|
|
|
peakname ( const char *filename )
|
2008-02-17 00:12:23 +01:00
|
|
|
{
|
2008-02-17 03:36:17 +01:00
|
|
|
static char file[512];
|
2008-02-17 00:12:23 +01:00
|
|
|
|
|
|
|
snprintf( file, 512, "%s.peak", filename );
|
|
|
|
|
2008-02-17 03:36:17 +01:00
|
|
|
return (const char*)&file;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-18 00:33:59 +01:00
|
|
|
make_peaks( 256 );
|
2008-02-17 03:59:45 +01:00
|
|
|
|
2008-02-17 03:36:17 +01:00
|
|
|
if ( ( fd = ::open( peakname( filename ), 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 21:41:59 +01:00
|
|
|
_peaks = (peakdata*)mmap( NULL, _len, PROT_READ, MAP_SHARED, fd, 0 );
|
2008-02-17 00:12:23 +01:00
|
|
|
|
2008-02-17 00:28:48 +01:00
|
|
|
::close( fd );
|
|
|
|
|
2008-02-17 00:12:23 +01:00
|
|
|
if ( _peaks == MAP_FAILED )
|
2008-02-17 03:59:45 +01:00
|
|
|
printf( "failed to create mapping!\n" );
|
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-02-18 00:33:59 +01:00
|
|
|
if ( ( pfd = ::open( peakname( _clip->name() ), O_RDONLY ) ) < 0 )
|
2008-02-17 03:59:45 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 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-02-18 00:29:09 +01:00
|
|
|
if ( ! _clip->open() )
|
|
|
|
return false;
|
2008-02-17 03:36:17 +01:00
|
|
|
|
|
|
|
FILE *fp = fopen( peakname( filename ), "w" );
|
|
|
|
|
|
|
|
if ( fp == NULL )
|
|
|
|
{
|
2008-02-18 00:29:09 +01:00
|
|
|
_clip->close();
|
2008-02-17 03:36:17 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write chunksize first */
|
|
|
|
fwrite( &chunksize, sizeof( int ), 1, fp );
|
|
|
|
|
|
|
|
size_t len;
|
|
|
|
do {
|
|
|
|
Peak p;
|
2008-02-18 00:29:09 +01:00
|
|
|
len = clip_read_peaks( &p, 1, chunksize );
|
2008-02-17 03:36:17 +01:00
|
|
|
fwrite( &p, sizeof( Peak ), 1, fp );
|
|
|
|
}
|
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
|
|
|
_clip->close();
|
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
|
|
|
}
|