Credit Andrew - fix vorbis audio which was scratchy and ensure aging plugin does...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / meterhistory.C
1 /*
2  * CINELERRA
3  * Copyright (C) 2016-2020 William Morrow
4  *
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.
9  *
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.
14  *
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
18  * USA
19  */
20
21 #include "meterhistory.h"
22
23 #include <stdlib.h>
24 #include <math.h>
25
26 MeterHistory::MeterHistory()
27 {
28         size = 0;
29         channels = 0;
30         current_peak = 0;
31         samples = 0;
32         values = 0;
33 }
34 MeterHistory::~MeterHistory()
35 {
36         init(0, 0);
37 }
38
39 void MeterHistory::init(int chs, int sz)
40 {
41         if( size != sz ) {
42                 delete [] samples;  samples = 0;
43                 size = 0;
44         }
45         if( !samples && sz > 0 ) {
46                 samples = new int64_t[size = sz];
47                 for( int i=0; i<size; ++i ) samples[i] = -1;
48         }
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;
53                 channels = 0;
54         }
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;
60         }
61 }
62
63 void MeterHistory::reset_channel(int ch)
64 {
65         if( !ch ) for( int i=0; i<size; ++i ) samples[i] = -1;
66         current_peak[ch] = 0;
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;
70 }
71
72 void MeterHistory::set_peak(int ch, double peak, int64_t pos)
73 {
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;
79 }
80
81 double MeterHistory::get_peak(int ch, int idx)
82 {
83         return idx>=0 ? values[ch][idx] : 0;
84 }
85
86 int MeterHistory::get_nearest(int64_t pos, int64_t tolerance)
87 {
88         int result = -1;
89         if( size > 0 ) {
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;
95                 }
96         }
97         return result;
98 }
99