3 * Copyright (C) 2016-2020 William Morrow
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 #include "meterhistory.h"
26 MeterHistory::MeterHistory()
34 MeterHistory::~MeterHistory()
39 void MeterHistory::init(int chs, int sz)
42 delete [] samples; samples = 0;
45 if( !samples && sz > 0 ) {
46 samples = new int64_t[size = sz];
47 for( int i=0; i<size; ++i ) samples[i] = -1;
49 if( channels != chs ) {
50 for( int i=0; i<channels; ++i ) delete [] values[i];
51 delete [] values; values = 0;
52 delete [] current_peak; current_peak = 0;
55 if( !values && chs > 0 ) {
56 current_peak = new int[channels = chs];
57 for( int i=0; i<channels; ++i ) current_peak[i] = 0;
58 values = new double*[channels];
59 for( int i=0; i<channels; ++i ) values[i] = 0;
63 void MeterHistory::reset_channel(int ch)
65 if( !ch ) for( int i=0; i<size; ++i ) samples[i] = -1;
67 double *peaks = values[ch];
68 if( !peaks ) values[ch] = peaks = new double[size];
69 for( int i=0; i<size; ++i ) peaks[i] = 0;
72 void MeterHistory::set_peak(int ch, double peak, int64_t pos)
74 int peak_idx = current_peak[ch];
75 samples[peak_idx] = pos;
76 values[ch][peak_idx++] = peak;
77 if( peak_idx >= size ) peak_idx = 0;
78 current_peak[ch] = peak_idx;
81 double MeterHistory::get_peak(int ch, int idx)
83 return idx>=0 ? values[ch][idx] : 0;
86 int MeterHistory::get_nearest(int64_t pos, int64_t tolerance)
90 int64_t best = tolerance;
91 for( int i=0; i<size; ++i ) {
92 int64_t diff = labs(samples[i] - pos);
93 if( diff >= tolerance || diff >= best ) continue;
94 best = diff; result = i;