Ardour  9.0-pre0-582-g084a23a80d
backends/portaudio/cycle_timer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef CYCLE_TIMER_H
20 #define CYCLE_TIMER_H
21 
22 #include <stdint.h>
23 #include <cmath>
24 #include <algorithm>
25 
26 // Could call it FrameTimer and make it more generic
27 // Could be an interface and or include clock source
28 // include sample count/processed samples in iteration?
29 class CycleTimer {
30 public:
32  : m_cycle_start (0)
33  , m_samplerate (0)
35  {
36  }
37 
38  void set_samplerate (double samplerate) { m_samplerate = samplerate; }
39 
40  double get_samplerate () const { return m_samplerate; }
41 
42  double get_sample_length_us () const { return 1e6 / m_samplerate; }
43 
44  double get_length_us () const
45  {
47  }
48 
49  void set_samples_per_cycle (uint32_t samples)
50  {
51  m_samples_per_cycle = samples;
52  }
53 
54  uint32_t get_samples_per_cycle () const { return m_samples_per_cycle; }
55 
56  // rint?? that may round to sample outside of cycle?
57  // max(0, value)?
58  uint32_t samples_since_cycle_start (int64_t timer_val) const
59  {
60  if (timer_val < m_cycle_start) {
61  return 0;
62  }
63  return std::max((double)0, (timer_val - get_start ()) / get_sample_length_us ());
64  }
65 
66  int64_t timestamp_from_sample_offset (uint32_t sample_offset)
67  {
68  return m_cycle_start + get_sample_length_us () * sample_offset;
69  }
70 
71  bool valid () const { return m_samples_per_cycle && m_samplerate; }
72 
73  bool in_cycle(int64_t timer_value_us) const
74  {
75  return (timer_value_us >= get_start()) && (timer_value_us < get_next_start());
76  }
77 
78  void reset_start (int64_t timestamp) { m_cycle_start = timestamp; }
79 
80  int64_t get_start () const { return m_cycle_start; }
81 
82  int64_t microseconds_since_start (int64_t timestamp) const
83  {
84  return timestamp - m_cycle_start;
85  }
86 
87  int64_t microseconds_since_start (uint32_t samples) const
88  {
89  return m_cycle_start + samples * get_sample_length_us ();
90  }
91 
92  int64_t get_next_start () const
93  {
94  return m_cycle_start + rint (get_length_us ());
95  }
96 
97 private:
98  int64_t m_cycle_start;
99 
100  uint32_t m_samplerate;
102 };
103 
104 #endif // CYCLE_TIMER_H
double get_sample_length_us() const
int64_t get_next_start() const
uint32_t get_samples_per_cycle() const
void set_samplerate(double samplerate)
uint32_t samples_since_cycle_start(int64_t timer_val) const
double get_length_us() const
bool in_cycle(int64_t timer_value_us) const
void set_samples_per_cycle(uint32_t samples)
int64_t microseconds_since_start(int64_t timestamp) const
double get_samplerate() const
int64_t microseconds_since_start(uint32_t samples) const
int64_t timestamp_from_sample_offset(uint32_t sample_offset)
void reset_start(int64_t timestamp)