Debian Codegraph @

src/clock.cc

raw
1 /*
2 * This file is part of MUSIC.
3 * Copyright (C) 2008, 2009 INCF
4 *
5 * MUSIC is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * MUSIC is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "music/clock.hh"
19
20 namespace MUSIC {
21
22 ClockState::ClockState (double t, double tb)
23 {
24 double dstate = t / tb;
25 if (dstate >= 0.0)
26 state = dstate + 0.5;
27 else
28 state = dstate - 0.5;
29 }
30
31
32 ClockState
33 ClockState::Serialized::deserialize ()
34 {
35 return (static_cast<long long> (upper) << 32) | lower;
36 }
37
38
39 ClockState::Serialized
40 ClockState::serialize ()
41 {
42 Serialized s;
43 s.upper = (state >> 32);
44 s.lower = state & 0xffffffffLL;
45 return s;
46 }
47
48
49 Clock::Clock (double tb, double h)
50 {
51 configure (tb, ClockState (h, tb));
52 }
53
54
55 void
56 Clock::configure (double tb, ClockState ti)
57 {
58 state_ = 0;
59 timebase_ = tb;
60 tickInterval_ = ti;
61 }
62
63
64 void
65 Clock::reset ()
66 {
67 state_ = 0;
68 }
69
70
71 void
72 Clock::tick ()
73 {
74 state_ += tickInterval_;
75 }
76
77
78 void
79 Clock::ticks (int n)
80 {
81 state_ += n * tickInterval_;
82 }
83
84
85 double
86 Clock::time () const
87 {
88 return timebase_ * state_;
89 }
90
91 }