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. */
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
|
2008-05-10 19:02:21 +02:00
|
|
|
|
|
|
|
|
|
/*
|
2008-05-11 02:10:49 +02:00
|
|
|
|
peakfile reading/writing.
|
2008-05-11 03:10:16 +02:00
|
|
|
|
*/
|
2008-05-10 19:02:21 +02:00
|
|
|
|
|
|
|
|
|
|
2008-02-17 00:12:23 +01:00
|
|
|
|
#include "Peaks.H"
|
2008-03-20 09:43:45 +01:00
|
|
|
|
|
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-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-05-10 19:02:21 +02:00
|
|
|
|
#include "debug.h"
|
2008-05-11 02:10:49 +02:00
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
2008-05-11 03:10:16 +02:00
|
|
|
|
#include "Transport.H" // for .recording
|
|
|
|
|
|
2008-05-11 02:10:49 +02:00
|
|
|
|
#include <list>
|
2008-05-18 07:09:18 +02:00
|
|
|
|
#include <algorithm>
|
|
|
|
|
using std::min;
|
|
|
|
|
using std::max;
|
|
|
|
|
|
2008-05-10 19:02:21 +02:00
|
|
|
|
|
2008-05-11 16:24:38 +02:00
|
|
|
|
/* whether to cache peaks at multiple resolutions on disk to
|
|
|
|
|
* drastically improve performance */
|
2008-05-10 20:19:55 +02:00
|
|
|
|
bool Peaks::mipmapped_peakfiles = true;
|
|
|
|
|
|
2008-05-12 01:28:08 +02:00
|
|
|
|
const int Peaks::cache_minimum = 256; /* minimum chunksize to build peakfiles for */
|
|
|
|
|
const int Peaks::cache_levels = 8; /* number of sampling levels in peak cache */
|
|
|
|
|
const int Peaks::cache_step = 1; /* powers of two between each level. 4 == 256, 2048, 16384, ... */
|
2008-02-17 21:41:59 +01:00
|
|
|
|
|
2008-05-10 20:19:55 +02:00
|
|
|
|
|
2008-05-11 16:24:38 +02:00
|
|
|
|
Peaks::peakbuffer Peaks::_peakbuf;
|
2008-05-10 20:19:55 +02:00
|
|
|
|
|
2008-05-11 16:24:38 +02:00
|
|
|
|
|
2008-05-10 20:19:55 +02:00
|
|
|
|
|
2008-04-17 21:19:41 +02:00
|
|
|
|
static
|
|
|
|
|
const char *
|
2008-05-11 02:10:49 +02:00
|
|
|
|
peakname ( const char *filename )
|
2008-04-17 21:19:41 +02:00
|
|
|
|
{
|
|
|
|
|
static char file[512];
|
|
|
|
|
|
2008-05-11 02:10:49 +02:00
|
|
|
|
snprintf( file, 512, "%s.peak", filename );
|
2008-04-17 21:19:41 +02:00
|
|
|
|
|
|
|
|
|
return (const char*)&file;
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-11 16:24:38 +02:00
|
|
|
|
/** update the modification time of file referred to by /fd/ */
|
|
|
|
|
static void
|
|
|
|
|
touch ( int fd )
|
2008-02-17 21:41:59 +01:00
|
|
|
|
{
|
2008-05-11 16:24:38 +02:00
|
|
|
|
struct stat st;
|
2008-03-07 00:21:57 +01:00
|
|
|
|
|
2008-05-11 16:24:38 +02:00
|
|
|
|
fstat( fd, &st );
|
|
|
|
|
|
|
|
|
|
fchmod( fd, st.st_mode );
|
2008-02-17 21:41:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
2008-05-11 16:24:38 +02: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
|
|
|
|
|
2008-05-10 19:02:21 +02:00
|
|
|
|
static nframes_t
|
|
|
|
|
nearest_cached_chunksize ( nframes_t chunksize )
|
|
|
|
|
{
|
|
|
|
|
nframes_t r = nearest_power_of_two( chunksize );
|
|
|
|
|
|
|
|
|
|
for ( int i = Peaks::cache_levels; i--; r >>= Peaks::cache_step )
|
|
|
|
|
if ( chunksize >= r )
|
|
|
|
|
return r;
|
2008-04-17 22:37:38 +02:00
|
|
|
|
|
2008-05-10 19:02:21 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-11 16:24:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Peaks::Peaks ( Audio_File *c )
|
|
|
|
|
{
|
|
|
|
|
_clip = c;
|
|
|
|
|
_peak_writer = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Peaks::~Peaks ( )
|
|
|
|
|
{
|
|
|
|
|
if ( _peak_writer )
|
|
|
|
|
delete _peak_writer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Prepare a buffer of peaks from /s/ to /e/ for reading. Must be
|
|
|
|
|
* called before any calls to operator[] */
|
|
|
|
|
int
|
|
|
|
|
Peaks::fill_buffer ( float fpp, nframes_t s, nframes_t e ) const
|
|
|
|
|
{
|
|
|
|
|
_fpp = fpp;
|
|
|
|
|
|
|
|
|
|
return read_peaks( s, e, (e - s) / fpp, fpp );
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-11 02:10:49 +02:00
|
|
|
|
struct peakfile_block_header
|
|
|
|
|
{
|
|
|
|
|
unsigned long chunksize;
|
|
|
|
|
unsigned long skip;
|
|
|
|
|
};
|
|
|
|
|
|
2008-05-10 19:02:21 +02:00
|
|
|
|
class Peakfile
|
2008-02-17 04:32:31 +01:00
|
|
|
|
{
|
2008-03-27 00:21:11 +01:00
|
|
|
|
|
2008-05-10 19:02:21 +02:00
|
|
|
|
FILE *_fp;
|
|
|
|
|
nframes_t _chunksize;
|
2008-05-11 02:10:49 +02:00
|
|
|
|
int _channels; /* number of channels this peakfile represents */
|
|
|
|
|
nframes_t _length; /* length, in frames, of the clip this peakfile represents */
|
|
|
|
|
size_t _offset;
|
2008-05-11 19:18:51 +02:00
|
|
|
|
int _blocks;
|
2008-05-11 02:10:49 +02:00
|
|
|
|
|
|
|
|
|
struct block_descriptor
|
|
|
|
|
{
|
|
|
|
|
nframes_t chunksize;
|
|
|
|
|
size_t pos;
|
|
|
|
|
|
|
|
|
|
block_descriptor ( nframes_t chunksize, size_t pos ) : chunksize( chunksize ), pos( pos )
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool operator< ( const block_descriptor &rhs )
|
|
|
|
|
{
|
|
|
|
|
return chunksize < rhs.chunksize;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2008-04-18 00:12:38 +02:00
|
|
|
|
|
2008-05-10 19:02:21 +02:00
|
|
|
|
public:
|
2008-04-18 00:12:38 +02:00
|
|
|
|
|
2008-05-10 19:02:21 +02:00
|
|
|
|
Peakfile ( )
|
|
|
|
|
{
|
2008-05-11 19:18:51 +02:00
|
|
|
|
_blocks = 0;
|
2008-05-10 19:02:21 +02:00
|
|
|
|
_fp = NULL;
|
2008-05-11 02:10:49 +02:00
|
|
|
|
_offset = 0;
|
2008-05-10 19:02:21 +02:00
|
|
|
|
_chunksize = 0;
|
|
|
|
|
_channels = 0;
|
|
|
|
|
}
|
2008-04-18 00:12:38 +02:00
|
|
|
|
|
2008-05-10 19:02:21 +02:00
|
|
|
|
~Peakfile ( )
|
|
|
|
|
{
|
|
|
|
|
if ( _fp )
|
|
|
|
|
close();
|
|
|
|
|
}
|
2008-04-18 00:12:38 +02:00
|
|
|
|
|
2008-05-11 19:18:51 +02:00
|
|
|
|
int blocks ( void ) const { return _blocks; }
|
2008-05-11 02:10:49 +02:00
|
|
|
|
/** find the best block for /chunksize/ */
|
|
|
|
|
void
|
2008-05-11 19:18:51 +02:00
|
|
|
|
scan ( nframes_t chunksize )
|
2008-05-11 02:10:49 +02:00
|
|
|
|
{
|
|
|
|
|
rewind( _fp );
|
|
|
|
|
clearerr( _fp );
|
|
|
|
|
|
|
|
|
|
std::list <block_descriptor> blocks;
|
|
|
|
|
|
|
|
|
|
/* scan all blocks */
|
2008-05-12 01:28:08 +02:00
|
|
|
|
for ( ;; )
|
2008-05-11 02:10:49 +02:00
|
|
|
|
{
|
|
|
|
|
peakfile_block_header bh;
|
|
|
|
|
|
|
|
|
|
fread( &bh, sizeof( bh ), 1, _fp );
|
|
|
|
|
|
2008-05-12 01:28:08 +02:00
|
|
|
|
if ( feof( _fp ) )
|
|
|
|
|
break;
|
|
|
|
|
|
2008-05-12 00:48:19 +02:00
|
|
|
|
// printf( "chunksize=%lu, skip=%lu\n", (unsigned long)bh.chunksize, (unsigned long) bh.skip );
|
|
|
|
|
|
2008-05-11 02:10:49 +02:00
|
|
|
|
ASSERT( bh.chunksize, "Invalid peak file structure!" );
|
|
|
|
|
|
|
|
|
|
blocks.push_back( block_descriptor( bh.chunksize, ftell( _fp ) ) );
|
|
|
|
|
|
|
|
|
|
if ( ! bh.skip )
|
2008-05-12 00:48:19 +02:00
|
|
|
|
/* last block */
|
2008-05-11 02:10:49 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if ( fseek( _fp, bh.skip, SEEK_CUR ) )
|
|
|
|
|
{
|
|
|
|
|
WARNING( "seek failed: %s (%lu)", strerror( errno ), bh.skip );
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ! blocks.size() )
|
|
|
|
|
FATAL( "invalid peak file?" );
|
|
|
|
|
|
|
|
|
|
// DMESSAGE( "peakfile has %d blocks.", blocks.size() );
|
|
|
|
|
|
|
|
|
|
blocks.sort();
|
|
|
|
|
|
|
|
|
|
/* fall back on the smallest chunksize */
|
|
|
|
|
fseek( _fp, blocks.front().pos, SEEK_SET );
|
|
|
|
|
_chunksize = blocks.front().chunksize;
|
|
|
|
|
|
|
|
|
|
/* search for the best-fit chunksize */
|
|
|
|
|
for ( std::list <block_descriptor>::const_reverse_iterator i = blocks.rbegin();
|
|
|
|
|
i != blocks.rend(); ++i )
|
|
|
|
|
if ( chunksize >= i->chunksize )
|
|
|
|
|
{
|
|
|
|
|
_chunksize = i->chunksize;
|
|
|
|
|
fseek( _fp, i->pos, SEEK_SET );
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-11 16:24:38 +02:00
|
|
|
|
// DMESSAGE( "using peakfile block for chunksize %lu", _chunksize );
|
2008-05-11 19:18:51 +02:00
|
|
|
|
_blocks = blocks.size();
|
2008-05-11 02:10:49 +02:00
|
|
|
|
_offset = ftell( _fp );
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-10 19:02:21 +02:00
|
|
|
|
/** convert frame number of peak number */
|
|
|
|
|
nframes_t frame_to_peak ( nframes_t frame )
|
|
|
|
|
{
|
|
|
|
|
return frame * _channels / _chunksize;
|
|
|
|
|
}
|
2008-03-27 01:31:57 +01:00
|
|
|
|
|
2008-05-10 19:02:21 +02:00
|
|
|
|
/** return the number of peaks in already open peakfile /fp/ */
|
|
|
|
|
nframes_t
|
|
|
|
|
npeaks ( void ) const
|
|
|
|
|
{
|
|
|
|
|
struct stat st;
|
2008-04-17 15:27:35 +02:00
|
|
|
|
|
2008-05-10 19:02:21 +02:00
|
|
|
|
fstat( fileno( _fp ), &st );
|
2008-03-27 01:31:57 +01:00
|
|
|
|
|
2008-05-11 19:18:51 +02:00
|
|
|
|
return ( st.st_size - sizeof( peakfile_block_header ) ) / sizeof( Peak );
|
2008-05-10 19:02:21 +02:00
|
|
|
|
}
|
2008-03-27 00:21:11 +01:00
|
|
|
|
|
2008-05-10 19:02:21 +02:00
|
|
|
|
/** returns true if the peakfile contains /npeaks/ peaks starting at sample /s/ */
|
|
|
|
|
bool
|
2008-05-11 19:18:51 +02:00
|
|
|
|
ready ( nframes_t start, nframes_t npeaks )
|
2008-05-10 19:02:21 +02:00
|
|
|
|
{
|
2008-05-11 19:18:51 +02:00
|
|
|
|
if ( _blocks > 1 )
|
|
|
|
|
return true;
|
|
|
|
|
else
|
|
|
|
|
return this->npeaks() > frame_to_peak( start ) + npeaks;
|
2008-05-10 19:02:21 +02:00
|
|
|
|
}
|
2008-03-27 01:31:57 +01:00
|
|
|
|
|
2008-05-10 19:02:21 +02:00
|
|
|
|
/** given soundfile name /name/, try to open the best peakfile for /chunksize/ */
|
|
|
|
|
bool
|
2008-05-12 00:48:19 +02:00
|
|
|
|
open ( const char *name, int channels, nframes_t chunksize )
|
2008-05-10 19:02:21 +02:00
|
|
|
|
{
|
2008-05-11 02:10:49 +02:00
|
|
|
|
_chunksize = 0;
|
2008-05-10 19:02:21 +02:00
|
|
|
|
_channels = channels;
|
2008-03-27 01:31:57 +01:00
|
|
|
|
|
2008-05-11 02:10:49 +02:00
|
|
|
|
if ( ! ( _fp = fopen( peakname( name ), "r" ) ) )
|
|
|
|
|
return false;
|
|
|
|
|
|
2008-05-11 19:18:51 +02:00
|
|
|
|
scan( chunksize );
|
2008-02-17 04:32:31 +01:00
|
|
|
|
|
2008-05-11 02:10:49 +02:00
|
|
|
|
assert( _chunksize );
|
|
|
|
|
|
|
|
|
|
return true;
|
2008-05-10 19:02:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
open ( FILE *fp, int channels, nframes_t chunksize )
|
|
|
|
|
{
|
|
|
|
|
_fp = fp;
|
2008-05-11 02:10:49 +02:00
|
|
|
|
_chunksize = 0;
|
2008-05-10 19:02:21 +02:00
|
|
|
|
_channels = channels;
|
2008-05-11 02:10:49 +02:00
|
|
|
|
|
2008-05-11 19:18:51 +02:00
|
|
|
|
scan( chunksize );
|
2008-05-11 02:10:49 +02:00
|
|
|
|
|
|
|
|
|
assert( _chunksize );
|
2008-05-10 19:02:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
leave_open ( void )
|
|
|
|
|
{
|
|
|
|
|
_fp = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
close ( void )
|
|
|
|
|
{
|
|
|
|
|
fclose( _fp );
|
|
|
|
|
_fp = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** read /npeaks/ peaks at /chunksize/ starting at sample /s/
|
|
|
|
|
* assuming the peakfile contains data for /channels/
|
|
|
|
|
* channels. Place the result in buffer /peaks/, which must be
|
|
|
|
|
* large enough to fit the entire request. Returns the number of
|
|
|
|
|
* peaks actually read, which may be fewer than were requested. */
|
2008-05-12 00:48:19 +02:00
|
|
|
|
nframes_t
|
2008-05-10 19:02:21 +02:00
|
|
|
|
read_peaks ( Peak *peaks, nframes_t s, int npeaks, nframes_t chunksize )
|
2008-03-27 01:31:57 +01:00
|
|
|
|
{
|
2008-05-10 19:02:21 +02:00
|
|
|
|
if ( ! _fp )
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
const unsigned int ratio = chunksize / _chunksize;
|
|
|
|
|
|
|
|
|
|
/* locate to start position */
|
2008-05-12 00:48:19 +02:00
|
|
|
|
if ( fseek( _fp, _offset + ( frame_to_peak( s ) * sizeof( Peak ) ), SEEK_SET ) )
|
2008-05-10 19:02:21 +02:00
|
|
|
|
/* failed to seek... peaks not ready? */
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if ( ratio == 1 )
|
2008-05-12 00:48:19 +02:00
|
|
|
|
return fread( peaks, sizeof( Peak ) * _channels, npeaks, _fp );
|
2008-03-27 00:21:11 +01:00
|
|
|
|
|
2008-05-10 19:02:21 +02:00
|
|
|
|
Peak *pbuf = new Peak[ ratio * _channels ];
|
2008-03-27 00:21:11 +01:00
|
|
|
|
|
2008-05-12 00:48:19 +02:00
|
|
|
|
nframes_t len = 0;
|
2008-04-17 18:30:20 +02:00
|
|
|
|
|
2008-05-12 00:48:19 +02:00
|
|
|
|
nframes_t i;
|
2008-05-10 19:02:21 +02:00
|
|
|
|
for ( i = 0; i < npeaks; ++i )
|
2008-03-27 01:31:57 +01:00
|
|
|
|
{
|
2008-05-10 19:02:21 +02:00
|
|
|
|
/* 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 ];
|
|
|
|
|
|
|
|
|
|
p->min = 0;
|
|
|
|
|
p->max = 0;
|
|
|
|
|
|
|
|
|
|
const Peak *pb = pbuf + j;
|
|
|
|
|
|
|
|
|
|
for ( int k = len; k--; pb += _channels )
|
|
|
|
|
{
|
|
|
|
|
if ( pb->max > p->max )
|
|
|
|
|
p->max = pb->max;
|
|
|
|
|
if ( pb->min < p->min )
|
|
|
|
|
p->min = pb->min;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( len < ratio )
|
|
|
|
|
break;
|
2008-03-27 01:31:57 +01:00
|
|
|
|
}
|
2008-02-17 04:32:31 +01:00
|
|
|
|
|
2008-05-10 19:02:21 +02:00
|
|
|
|
delete[] pbuf;
|
|
|
|
|
|
|
|
|
|
return i;
|
2008-03-27 01:31:57 +01:00
|
|
|
|
}
|
2008-05-10 19:02:21 +02:00
|
|
|
|
};
|
|
|
|
|
|
2008-05-11 19:18:51 +02:00
|
|
|
|
bool
|
|
|
|
|
Peaks::ready ( nframes_t s, int npeaks, nframes_t chunksize ) const
|
|
|
|
|
{
|
|
|
|
|
Peakfile _peakfile;
|
|
|
|
|
|
2008-05-12 00:48:19 +02:00
|
|
|
|
if ( ! _peakfile.open( _clip->name(), _clip->channels(), chunksize ) )
|
2008-05-11 19:18:51 +02:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return _peakfile.ready( s, npeaks );
|
|
|
|
|
}
|
2008-05-10 19:02:21 +02:00
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
Peaks::read_peakfile_peaks ( Peak *peaks, nframes_t s, int npeaks, nframes_t chunksize ) const
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
nframes_t ncc = nearest_cached_chunksize( chunksize );
|
|
|
|
|
|
2008-05-11 03:10:16 +02:00
|
|
|
|
/* never try to build peaks while recording */
|
2008-05-12 00:48:19 +02:00
|
|
|
|
if ( ! transport->recording )
|
2008-05-11 03:10:16 +02:00
|
|
|
|
{
|
|
|
|
|
if ( ! current() )
|
|
|
|
|
/* Build peaks asyncronously */
|
|
|
|
|
if ( ! fork() )
|
|
|
|
|
exit( make_peaks() );
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2008-05-10 19:02:21 +02:00
|
|
|
|
|
|
|
|
|
Peakfile _peakfile;
|
|
|
|
|
|
2008-05-12 00:48:19 +02:00
|
|
|
|
if ( ! _peakfile.open( _clip->name(), _clip->channels(), chunksize ) )
|
2008-05-10 19:02:21 +02:00
|
|
|
|
return 0;
|
2008-05-11 19:18:51 +02:00
|
|
|
|
|
|
|
|
|
/* else if ( ! _peakfile.contains( s, npeaks ) ) */
|
|
|
|
|
/* { */
|
|
|
|
|
/* /\* the best peakfile for this chunksize doesn't have the */
|
|
|
|
|
/* * peaks we need. Perhaps it's still being constructed, */
|
|
|
|
|
/* * try the next best, then give up. *\/ */
|
|
|
|
|
/* if ( ! _peakfile.open( _clip->name(), chunksize >> 1, _clip->channels() ) ) */
|
|
|
|
|
/* return 0; */
|
|
|
|
|
/* } */
|
2008-03-27 00:21:11 +01:00
|
|
|
|
|
2008-05-10 19:02:21 +02:00
|
|
|
|
return _peakfile.read_peaks( peaks, s, npeaks, chunksize );
|
2008-03-27 00:21:11 +01:00
|
|
|
|
|
2008-05-10 19:02:21 +02:00
|
|
|
|
// _peakfile.close();
|
2008-03-27 00:21:11 +01:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-10 19:02:21 +02:00
|
|
|
|
|
2008-03-27 00:21:11 +01:00
|
|
|
|
int
|
2008-05-10 19:02:21 +02:00
|
|
|
|
Peaks::read_source_peaks ( Peak *peaks, int npeaks, nframes_t 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;
|
|
|
|
|
|
2008-05-08 01:19:48 +02:00
|
|
|
|
for ( nframes_t k = j; k < len * channels; k += channels )
|
2008-03-27 00:21:11 +01:00
|
|
|
|
{
|
|
|
|
|
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-05-08 01:19:48 +02:00
|
|
|
|
if ( len < (nframes_t)chunksize )
|
2008-02-17 10:00:38 +01:00
|
|
|
|
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
|
2008-05-10 19:02:21 +02:00
|
|
|
|
Peaks::read_source_peaks ( Peak *peaks, nframes_t s, int npeaks, nframes_t chunksize ) const
|
2008-03-27 01:46:38 +01:00
|
|
|
|
{
|
|
|
|
|
_clip->seek( s );
|
|
|
|
|
|
|
|
|
|
int i = read_source_peaks( peaks, npeaks, chunksize );
|
|
|
|
|
|
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-05-10 19:02:21 +02:00
|
|
|
|
Peaks::read_peaks ( nframes_t s, nframes_t e, int npeaks, nframes_t chunksize ) const
|
2008-02-17 10:00:38 +01:00
|
|
|
|
{
|
2008-05-10 19:02:21 +02:00
|
|
|
|
// printf( "reading peaks %d @ %d\n", npeaks, chunksize );
|
2008-02-17 04:32:31 +01:00
|
|
|
|
|
2008-05-08 01:19:48 +02:00
|
|
|
|
if ( _peakbuf.size < (nframes_t)( 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-03-25 22:17:24 +01:00
|
|
|
|
_peakbuf.offset = s;
|
|
|
|
|
_peakbuf.buf->chunksize = chunksize;
|
2008-03-27 01:31:57 +01:00
|
|
|
|
|
2008-05-11 16:24:38 +02:00
|
|
|
|
/* FIXME: use actual minimum chunksize from peakfile! */
|
|
|
|
|
if ( chunksize < cache_minimum )
|
2008-03-27 01:31:57 +01:00
|
|
|
|
_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: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-05-11 03:10:16 +02: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-05-11 02:10:49 +02:00
|
|
|
|
if ( ( pfd = ::open( peakname( _clip->name() ), 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-02-18 04:37:26 +01:00
|
|
|
|
|
2008-05-11 02:10:49 +02:00
|
|
|
|
bool
|
|
|
|
|
Peaks::make_peaks ( void ) const
|
|
|
|
|
{
|
2008-05-11 16:24:38 +02:00
|
|
|
|
Peaks::Builder pb( this );
|
2008-05-11 02:10:49 +02:00
|
|
|
|
|
2008-05-11 03:40:43 +02:00
|
|
|
|
return pb.make_peaks();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
Peaks::make_peaks_mipmap ( void ) const
|
|
|
|
|
{
|
2008-05-11 16:24:38 +02:00
|
|
|
|
Peaks::Builder pb( this );
|
2008-05-11 03:40:43 +02:00
|
|
|
|
|
|
|
|
|
return pb.make_peaks_mipmap();
|
2008-05-11 02:10:49 +02:00
|
|
|
|
}
|
2008-02-18 04:37:26 +01:00
|
|
|
|
|
2008-05-10 19:02:21 +02:00
|
|
|
|
/** return normalization factor for a single peak, assuming the peak
|
|
|
|
|
* represents a downsampling of the entire range to be normalized. */
|
2008-05-01 09:13:58 +02:00
|
|
|
|
float
|
|
|
|
|
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
|
|
|
|
|
2008-05-12 00:48:19 +02:00
|
|
|
|
/* THREAD: IO */
|
2008-05-10 20:19:55 +02:00
|
|
|
|
/* wrapper for peak writer */
|
|
|
|
|
void
|
|
|
|
|
Peaks::prepare_for_writing ( void )
|
|
|
|
|
{
|
|
|
|
|
assert( ! _peak_writer );
|
|
|
|
|
|
2008-05-12 00:48:19 +02:00
|
|
|
|
_peak_writer = new Peaks::Streamer( _clip->name(), _clip->channels(), cache_minimum );
|
2008-05-10 20:19:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-05-12 00:48:19 +02:00
|
|
|
|
/* THREAD: IO */
|
2008-05-11 03:10:16 +02:00
|
|
|
|
void
|
|
|
|
|
Peaks::finish_writing ( void )
|
|
|
|
|
{
|
2008-05-12 03:09:39 +02:00
|
|
|
|
assert( _peak_writer );
|
|
|
|
|
|
|
|
|
|
delete _peak_writer;
|
|
|
|
|
_peak_writer = NULL;
|
2008-05-11 03:40:43 +02:00
|
|
|
|
|
|
|
|
|
/* now fill in the rest of the cache */
|
2008-05-17 18:23:37 +02:00
|
|
|
|
make_peaks_mipmap();
|
|
|
|
|
/* if ( ! fork() ) */
|
|
|
|
|
/* exit( make_peaks_mipmap() ); */
|
|
|
|
|
|
2008-05-11 03:10:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-05-12 00:48:19 +02:00
|
|
|
|
/* THREAD: IO */
|
2008-05-10 20:19:55 +02:00
|
|
|
|
void
|
|
|
|
|
Peaks::write ( sample_t *buf, nframes_t nframes )
|
|
|
|
|
{
|
|
|
|
|
_peak_writer->write( buf, nframes );
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-11 16:24:38 +02:00
|
|
|
|
|
2008-05-10 20:19:55 +02:00
|
|
|
|
|
2008-05-11 16:24:38 +02:00
|
|
|
|
/*
|
|
|
|
|
The Streamer is for streaming peaks from audio buffers to disk while
|
|
|
|
|
capturing. It works by accumulating a peak value across write()
|
|
|
|
|
calls. The Streamer can only generate peaks at a single
|
|
|
|
|
chunksize--additional cache levels must be appended after the
|
|
|
|
|
Streamer has finished.
|
|
|
|
|
*/
|
|
|
|
|
|
2008-05-12 00:48:19 +02:00
|
|
|
|
Peaks::Streamer::Streamer ( const char *filename, int channels, nframes_t chunksize )
|
2008-04-17 21:19:41 +02:00
|
|
|
|
{
|
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-05-12 00:48:19 +02:00
|
|
|
|
_fp = NULL;
|
2008-04-17 21:19:41 +02:00
|
|
|
|
|
|
|
|
|
_peak = new Peak[ channels ];
|
|
|
|
|
memset( _peak, 0, sizeof( Peak ) * channels );
|
|
|
|
|
|
2008-05-11 02:10:49 +02:00
|
|
|
|
if ( ! ( _fp = fopen( peakname( filename ), "w" ) ) )
|
2008-05-12 00:48:19 +02:00
|
|
|
|
{
|
|
|
|
|
WARNING( "could not open peakfile for streaming." );
|
|
|
|
|
}
|
2008-05-11 02:10:49 +02:00
|
|
|
|
|
|
|
|
|
peakfile_block_header bh;
|
|
|
|
|
|
|
|
|
|
bh.chunksize = chunksize;
|
|
|
|
|
bh.skip = 0;
|
|
|
|
|
|
|
|
|
|
fwrite( &bh, sizeof( bh ), 1, _fp );
|
|
|
|
|
|
|
|
|
|
fflush( _fp );
|
2008-04-17 21:19:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-05-11 16:24:38 +02:00
|
|
|
|
Peaks::Streamer::~Streamer ( )
|
2008-04-17 21:19:41 +02:00
|
|
|
|
{
|
2008-05-12 04:01:51 +02:00
|
|
|
|
/* fwrite( _peak, sizeof( Peak ) * _channels, 1, _fp ); */
|
2008-05-12 00:48:19 +02:00
|
|
|
|
|
2008-05-11 03:10:16 +02:00
|
|
|
|
touch( fileno( _fp ) );
|
|
|
|
|
|
2008-04-17 21:19:41 +02:00
|
|
|
|
fclose( _fp );
|
2008-05-11 03:10:16 +02:00
|
|
|
|
|
2008-05-10 20:19:55 +02:00
|
|
|
|
delete[] _peak;
|
2008-04-17 21:19:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** append peaks for samples in /buf/ to peakfile */
|
|
|
|
|
void
|
2008-05-12 04:01:51 +02:00
|
|
|
|
Peaks::Streamer::write ( const sample_t *buf, nframes_t nframes )
|
2008-04-17 21:19:41 +02:00
|
|
|
|
{
|
2008-05-12 04:01:51 +02:00
|
|
|
|
while ( nframes )
|
2008-04-17 21:19:41 +02:00
|
|
|
|
{
|
2008-05-12 04:01:51 +02:00
|
|
|
|
const nframes_t remaining = _chunksize - _index;
|
2008-04-17 21:19:41 +02:00
|
|
|
|
|
2008-05-12 04:01:51 +02:00
|
|
|
|
if ( ! remaining )
|
2008-04-17 21:19:41 +02:00
|
|
|
|
{
|
2008-05-12 00:48:19 +02:00
|
|
|
|
fwrite( _peak, sizeof( Peak ) * _channels, 1, _fp );
|
|
|
|
|
|
2008-05-12 04:01:51 +02:00
|
|
|
|
/* FIXME: shouldn't we just use write() instead? */
|
2008-05-12 00:48:19 +02:00
|
|
|
|
fflush( _fp );
|
|
|
|
|
|
2008-04-17 21:19:41 +02:00
|
|
|
|
memset( _peak, 0, sizeof( Peak ) * _channels );
|
2008-05-12 04:01:51 +02:00
|
|
|
|
|
2008-04-17 22:37:38 +02:00
|
|
|
|
_index = 0;
|
2008-04-17 21:19:41 +02:00
|
|
|
|
}
|
2008-05-12 04:01:51 +02:00
|
|
|
|
|
|
|
|
|
int processed = min( nframes, remaining );
|
|
|
|
|
|
|
|
|
|
for ( int i = _channels; i--; )
|
|
|
|
|
{
|
|
|
|
|
Peak *p = _peak + i;
|
|
|
|
|
|
|
|
|
|
const sample_t *f = buf + i;
|
|
|
|
|
|
|
|
|
|
for ( int j = processed; j--; f += _channels )
|
|
|
|
|
{
|
|
|
|
|
if ( *f > p->max )
|
|
|
|
|
p->max = *f;
|
|
|
|
|
if ( *f < p->min )
|
|
|
|
|
p->min = *f;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_index += processed;
|
|
|
|
|
nframes -= processed;
|
2008-04-17 21:19:41 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2008-05-11 16:24:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
The Builder is for generating peaks from imported or updated
|
|
|
|
|
sources, or when the peakfile is simply missing.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Peaks::Builder::write_block_header ( nframes_t chunksize )
|
|
|
|
|
{
|
|
|
|
|
if ( last_block_pos )
|
|
|
|
|
{
|
|
|
|
|
/* update previous block */
|
|
|
|
|
size_t pos = ftell( fp );
|
|
|
|
|
|
|
|
|
|
fseek( fp, last_block_pos - sizeof( peakfile_block_header ), SEEK_SET );
|
|
|
|
|
|
|
|
|
|
peakfile_block_header bh;
|
|
|
|
|
|
|
|
|
|
fread( &bh, sizeof( bh ), 1, fp );
|
|
|
|
|
|
|
|
|
|
fseek( fp, last_block_pos - sizeof( peakfile_block_header ), SEEK_SET );
|
|
|
|
|
// fseek( fp, 0 - sizeof( bh ), SEEK_CUR );
|
|
|
|
|
|
2008-05-12 00:48:19 +02:00
|
|
|
|
// DMESSAGE( "old block header: chunksize=%lu, skip=%lu", bh.chunksize, bh.skip );
|
2008-05-11 16:24:38 +02:00
|
|
|
|
|
|
|
|
|
bh.skip = pos - last_block_pos;
|
|
|
|
|
|
2008-05-12 00:48:19 +02:00
|
|
|
|
ASSERT( bh.skip, "Attempt to create empty block. pos=%lu, last_block_pos=%lu", pos, last_block_pos );
|
|
|
|
|
|
|
|
|
|
// DMESSAGE( "new block header: chunksize=%lu, skip=%lu", bh.chunksize, bh.skip );
|
2008-05-11 16:24:38 +02:00
|
|
|
|
|
|
|
|
|
fwrite( &bh, sizeof( bh ), 1, fp );
|
|
|
|
|
|
|
|
|
|
fseek( fp, pos, SEEK_SET );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
peakfile_block_header bh;
|
|
|
|
|
|
|
|
|
|
bh.chunksize = chunksize;
|
|
|
|
|
bh.skip = 0;
|
|
|
|
|
|
|
|
|
|
fwrite( &bh, sizeof( bh ), 1, fp );
|
|
|
|
|
|
|
|
|
|
last_block_pos = ftell( fp );
|
2008-05-11 19:18:51 +02:00
|
|
|
|
|
|
|
|
|
fflush( fp );
|
2008-05-11 16:24:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** generate additional cache levels for a peakfile with only 1 block (ie. that of a new capture) */
|
|
|
|
|
bool
|
|
|
|
|
Peaks::Builder::make_peaks_mipmap ( void )
|
|
|
|
|
{
|
|
|
|
|
if ( ! Peaks::mipmapped_peakfiles )
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
Audio_File *_clip = _peaks->_clip;
|
|
|
|
|
|
|
|
|
|
const char *filename = _clip->name();
|
|
|
|
|
|
|
|
|
|
FILE *rfp;
|
|
|
|
|
|
2008-05-12 00:48:19 +02:00
|
|
|
|
rfp = fopen( peakname( filename ), "r" );
|
|
|
|
|
|
2008-05-11 16:24:38 +02:00
|
|
|
|
last_block_pos = sizeof( peakfile_block_header );
|
|
|
|
|
|
|
|
|
|
/* open for reading */
|
2008-05-12 00:48:19 +02:00
|
|
|
|
// rfp = fopen( peakname( filename ), "r" );
|
2008-05-11 16:24:38 +02:00
|
|
|
|
/* open the file again for appending */
|
2008-05-12 00:48:19 +02:00
|
|
|
|
if ( ! ( fp = fopen( peakname( filename ), "r+" ) ) )
|
|
|
|
|
{
|
|
|
|
|
WARNING( "could not open peakfile for appending." );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( fseek( fp, 0, SEEK_END ) )
|
|
|
|
|
FATAL( "error performing seek: %s", strerror( errno ) );
|
|
|
|
|
|
|
|
|
|
if ( ftell( fp ) == sizeof( peakfile_block_header ) )
|
|
|
|
|
{
|
|
|
|
|
DWARNING( "truncated peakfile. Programming error?" );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2008-05-11 16:24:38 +02:00
|
|
|
|
|
|
|
|
|
Peak buf[ _clip->channels() ];
|
|
|
|
|
|
|
|
|
|
/* now build the remaining peak levels, each based on the
|
|
|
|
|
* preceding level */
|
|
|
|
|
|
|
|
|
|
nframes_t cs = Peaks::cache_minimum << Peaks::cache_step;
|
|
|
|
|
for ( int i = 1; i < Peaks::cache_levels; ++i, cs <<= Peaks::cache_step )
|
|
|
|
|
{
|
|
|
|
|
DMESSAGE( "building level %d peak cache", i + 1 );
|
|
|
|
|
|
2008-05-12 00:48:19 +02:00
|
|
|
|
DMESSAGE( "%lu", _clip->length() / cs );
|
|
|
|
|
|
|
|
|
|
if ( _clip->length() / cs < 1 )
|
|
|
|
|
{
|
|
|
|
|
DMESSAGE( "source not long enough for any peaks at chunksize %lu", cs );
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-05-11 16:24:38 +02:00
|
|
|
|
Peakfile pf;
|
|
|
|
|
|
|
|
|
|
/* open the peakfile for the previous cache level */
|
|
|
|
|
pf.open( rfp, _clip->channels(), cs >> Peaks::cache_step );
|
|
|
|
|
|
2008-05-12 00:48:19 +02:00
|
|
|
|
// pf.open( _clip->name(), _clip->channels(), cs >> Peaks::cache_step );
|
|
|
|
|
|
2008-05-11 16:24:38 +02:00
|
|
|
|
write_block_header( cs );
|
|
|
|
|
|
|
|
|
|
size_t len;
|
|
|
|
|
nframes_t s = 0;
|
|
|
|
|
do {
|
|
|
|
|
len = pf.read_peaks( buf, s, 1, cs );
|
|
|
|
|
s += cs;
|
|
|
|
|
|
|
|
|
|
fwrite( buf, sizeof( buf ), len, fp );
|
|
|
|
|
}
|
|
|
|
|
while ( len );
|
|
|
|
|
|
|
|
|
|
pf.leave_open();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fclose( rfp );
|
|
|
|
|
fclose( fp );
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
Peaks::Builder::make_peaks ( void )
|
|
|
|
|
{
|
|
|
|
|
Audio_File *_clip = _peaks->_clip;
|
|
|
|
|
|
|
|
|
|
const char *filename = _clip->name();
|
|
|
|
|
|
|
|
|
|
DMESSAGE( "building peaks for \"%s\"", filename );
|
|
|
|
|
|
|
|
|
|
if ( ! ( fp = fopen( peakname( filename ), "w+" ) ) )
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
_clip->seek( 0 );
|
|
|
|
|
|
|
|
|
|
Peak buf[ _clip->channels() ];
|
|
|
|
|
|
|
|
|
|
DMESSAGE( "building level 1 peak cache" );
|
|
|
|
|
|
|
|
|
|
write_block_header( Peaks::cache_minimum );
|
|
|
|
|
|
|
|
|
|
/* build first level from source */
|
|
|
|
|
size_t len;
|
|
|
|
|
do {
|
|
|
|
|
len = _peaks->read_source_peaks( buf, 1, Peaks::cache_minimum );
|
|
|
|
|
|
|
|
|
|
fwrite( buf, sizeof( buf ), len, fp );
|
|
|
|
|
}
|
|
|
|
|
while ( len );
|
|
|
|
|
|
|
|
|
|
/* reopen for reading */
|
|
|
|
|
fclose( fp );
|
|
|
|
|
|
|
|
|
|
make_peaks_mipmap();
|
|
|
|
|
|
|
|
|
|
DMESSAGE( "done building peaks" );
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Peaks::Builder::Builder ( const Peaks *peaks ) : _peaks( peaks )
|
|
|
|
|
{
|
|
|
|
|
fp = NULL;
|
|
|
|
|
last_block_pos = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|