fixes and accomodations for other operating systems by Andrew
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / meterhistory.C
1 #include "meterhistory.h"
2
3 #include <stdlib.h>
4 #include <math.h>
5
6 MeterHistory::MeterHistory()
7 {
8         size = 0;
9         channels = 0;
10         current_peak = 0;
11         samples = 0;
12         values = 0;
13 }
14 MeterHistory::~MeterHistory()
15 {
16         init(0, 0);
17 }
18
19 void MeterHistory::init(int chs, int sz)
20 {
21         if( size != sz ) {
22                 delete [] samples;  samples = 0;
23                 size = 0;
24         }
25         if( !samples && sz > 0 ) {
26                 samples = new int64_t[size = sz];
27                 for( int i=0; i<size; ++i ) samples[i] = -1;
28         }
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;
33                 channels = 0;
34         }
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;
40         }
41 }
42
43 void MeterHistory::reset_channel(int ch)
44 {
45         if( !ch ) for( int i=0; i<size; ++i ) samples[i] = -1;
46         current_peak[ch] = 0;
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;
50 }
51
52 void MeterHistory::set_peak(int ch, double peak, int64_t pos)
53 {
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;
59 }
60
61 double MeterHistory::get_peak(int ch, int idx)
62 {
63         return idx>=0 ? values[ch][idx] : 0;
64 }
65
66 int MeterHistory::get_nearest(int64_t pos, int64_t tolerance)
67 {
68         int result = -1;
69         if( size > 0 ) {
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;
75                 }
76         }
77         return result;
78 }
79