NiCMidi 1.1.0
A MIDI library derived from J.D.Koftinoff jdksmidi
timer.h
Go to the documentation of this file.
1/*
2 * NiCMidi - A C++ Class Library for MIDI
3 *
4 * Copyright (C) 2004 J.D. Koftinoff Software, Ltd.
5 * www.jdkoftinoff.com jeffk@jdkoftinoff.com
6 * Copyright (C) 2021, 2022 Nicola Cassetta
7 * https://github.com/ncassetta/NiCMidi
8 *
9 * This file is part of NiCMidi.
10 *
11 * NiCMidi is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as
13 * published by the Free Software Foundation, either version 3 of
14 * the License, or (at your option) any later version.
15 *
16 * NiCMidi is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with NiCMidi. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25
28
29
30#ifndef _NICMIDI_TIMER_H
31#define _NICMIDI_TIMER_H
32
33#include <chrono>
34#include <thread>
35#include <atomic>
36
37
38
39
42
44typedef unsigned long long tMsecs;
47typedef void (MIDITick)(tMsecs, void*);
49
50
62class MIDITimer {
63 public:
64
66 typedef std::chrono::steady_clock::time_point timepoint;
68 typedef std::chrono::milliseconds duration;
70 MIDITimer() = delete;
72 static unsigned int GetResolution() { return resolution; }
74 static MIDITick* GetMIDITick() { return tick_proc; }
76 static bool IsOpen() { return (num_open > 0); }
77
80 static void SetResolution(unsigned int res);
85 static void SetMIDITick(MIDITick* t, void* tp = 0);
86
90 static bool Start();
93 static void Stop();
96 static void HardStop();
97
101 { return std::chrono::duration_cast<std::chrono::milliseconds>
102 (std::chrono::steady_clock::now() - sys_clock_base).count(); }
105 static void Wait(unsigned int msecs)
106 { std::this_thread::sleep_for(std::chrono::milliseconds(msecs)); }
107
108 protected:
109
110 static const unsigned int DEFAULT_RESOLUTION = 10;
114 static void ThreadProc();
115
117 static unsigned int resolution; // The actual timer resolution
118 static MIDITick* tick_proc; // The callback function set by the user
119 static void* tick_param; // The callback second parameter set by the user
120 static std::thread bg_thread; // The background thread
121 static std::atomic<int> num_open; // The number of times Start() was called without a corresponding Stop()
122 static const timepoint sys_clock_base; // The base timepoint for calculating system time
123 static timepoint current; // Internal use
125};
126
127//extern MIDITimer main_timer;
128
129#endif // TIMER_H_INCLUDED
A static class which provides the timing required for MIDI playback, using the C++11 <chrono> methods...
Definition: timer.h:62
static MIDITick * GetMIDITick()
Returns the pointer to the callback function set by the user.
Definition: timer.h:74
static bool IsOpen()
Returns true if the timer is running.
Definition: timer.h:76
static void HardStop()
Stops the timer, joining the background thread procedure, regardless the number of times Start() was ...
MIDITimer()=delete
The constructor is deleted.
std::chrono::steady_clock::time_point timepoint
Type for a variable which can hold a specific time point (internal use).
Definition: timer.h:66
static const unsigned int DEFAULT_RESOLUTION
The default timer resolution.
Definition: timer.h:110
static unsigned int GetResolution()
Returns the timer resolution, i.e. the time interval (in milliseconds) between two ticks.
Definition: timer.h:72
static tMsecs GetSysTimeMs()
Returns the elapsed time in milliseconds since the start of application.
Definition: timer.h:100
static void SetResolution(unsigned int res)
Sets the timer resolution to the given value in milliseconds.
static void SetMIDITick(MIDITick *t, void *tp=0)
Sets the callback function to be called at every timer tick and its parameter.
static void ThreadProc()
The background thread procedure.
static void Stop()
Stops the timer, joining the background thread procedure.
static void Wait(unsigned int msecs)
Stops the calling thread for the given number of milliseconds.
Definition: timer.h:105
std::chrono::milliseconds duration
Type for a variable which can hold a time duration (in milliseconds).
Definition: timer.h:68
static bool Start()
Starts the background thread procedure which calls the callback function at every timer tick.
unsigned long long tMsecs
The type of a variable which can hold the elapsed time in milliseconds.
Definition: timer.h:44
void() MIDITick(tMsecs, void *)
This is the typedef of the callback functions which are called at every timer tick.
Definition: timer.h:47