1 #include "meterhistory.h"
6 MeterHistory::MeterHistory()
14 MeterHistory::~MeterHistory()
19 void MeterHistory::init(int chs, int sz)
22 delete [] samples; samples = 0;
25 if( !samples && sz > 0 ) {
26 samples = new int64_t[size = sz];
27 for( int i=0; i<size; ++i ) samples[i] = -1;
29 if( channels != chs ) {
30 for( int i=0; i<channels; ++i ) delete [] values[i];
31 delete [] values; values = 0;
32 delete [] current_peak; current_peak = 0;
35 if( !values && chs > 0 ) {
36 current_peak = new int[channels = chs];
37 for( int i=0; i<channels; ++i ) current_peak[i] = 0;
38 values = new double*[channels];
39 for( int i=0; i<channels; ++i ) values[i] = 0;
43 void MeterHistory::reset_channel(int ch)
45 if( !ch ) for( int i=0; i<size; ++i ) samples[i] = -1;
47 double *peaks = values[ch];
48 if( !peaks ) values[ch] = peaks = new double[size];
49 for( int i=0; i<size; ++i ) peaks[i] = 0;
52 void MeterHistory::set_peak(int ch, double peak, int64_t pos)
54 int peak_idx = current_peak[ch];
55 samples[peak_idx] = pos;
56 values[ch][peak_idx++] = peak;
57 if( peak_idx >= size ) peak_idx = 0;
58 current_peak[ch] = peak_idx;
61 double MeterHistory::get_peak(int ch, int idx)
63 return idx>=0 ? values[ch][idx] : 0;
66 int MeterHistory::get_nearest(int64_t pos, int64_t tolerance)
70 int64_t best = tolerance;
71 for( int i=0; i<size; ++i ) {
72 int64_t diff = labs(samples[i] - pos);
73 if( diff >= tolerance || diff >= best ) continue;
74 best = diff; result = i;