aboutsummaryrefslogtreecommitdiff
path: root/src/midiio/include/SigTimer.h
blob: e93345c0f769f26c548126e353e0a484c837a858 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Thanks to:     Erik Neuenschwander <erikn@leland.stanford.edu>
//                   for Windows 95 assembly code for Pentium clock cycles.
//                Ozgur Izmirli <ozgur@ccrma.stanford.edu> 
//                   for concept of periodic timer.
// Creation Date: Mon Oct 13 11:34:57 GMT-0800 1997
// Last Modified: Tue Feb 10 21:05:19 GMT-0800 1998
// Last Modified: Sat Sep 19 15:56:48 PDT 1998
// Last Modified: Mon Feb 22 04:44:25 PST 1999
// Last Modified: Sun Nov 28 12:39:39 PST 1999   (added adjustPeriod())
// Filename:      .../sig/code/control/SigTimer/SigTimer.h
// Web Address:   http://www-ccrma.stanford.edu/~craig/improv/include/SigTimer.h
// Syntax:        C++ 
//
// Description:   This class can only be used on Motorola Pentinum 75 Mhz
//                chips or better because the timing information is
//                extracted from the clock cycle count from a register
//                on the CPU itself.  This class will estimate the 
//                speed of the computer, but it would be better if there
//                was a way of finding out the speed from some function.
//                This class is used primarily for timing of MIDI input 
//                and output at a millisecond resolution.
//
// Interesting:   http://www.datasilicon.nl/I786/timer_1.htm
//

#ifndef _SIGTIMER_H_INCLUDED
#define _SIGTIMER_H_INCLUDED


#ifdef VISUAL
   #include <wtypes.h>
   typedef LONGLONG int64bits;
#else
   typedef long long int int64bits;
   #include <unistd.h>                 /* for millisleep function */
#endif


class SigTimer {
   public:
                       SigTimer           (void);
                       SigTimer           (int aSpeed);
                       SigTimer           (SigTimer& aTimer);
                      ~SigTimer           ();

      void             adjustPeriod       (double periodDelta);
      int              expired            (void) const;
      double           getPeriod          (void) const;
      double           getPeriodCount     (void) const;
      double           getTempo           (void) const;
      int              getTicksPerSecond  (void) const;
      int              getTime            (void) const;
      double           getTimeInSeconds   (void) const;
      int              getTimeInTicks     (void) const;
      void             reset              (void);
      void             setPeriod          (double aPeriod);
      void             setTempo           (double beatsPerMinute);
      void             setPeriodCount     (double aCount);
      void             setTicksPerSecond  (int aTickRate);
      void             start              (void);
      void             sync               (SigTimer& aTimer);
      void             update             (void);
      void             update             (int periodCount);

      // The following functions are semi-private.  They do not have
      // anything to do with timing themselves, but are a by-product
      // of the timer implementation.  They are useful, so they have
      // been left public; however, they should be used judiciously.
      static int       getCpuSpeed        (void);
      static int       measureCpuSpeed    (int quantize = 0);
      static void      setCpuSpeed        (int aSpeed);
      
      // the following function is hardware specific to Intel Pentium
      // computers with a processor speed of at least 75 MHz.
      // This function is the only non-portable function in this
      // class, but everything else is based on it.
      static int64bits clockCycles        (void);

   protected:
      static int64bits globalOffset;
      static int       cpuSpeed;         

      int64bits        offset;          
      int              ticksPerSecond;    
      double           period;

   // protected functions
      double           getFactor          (void) const;

};


// The following function is mostly for Linux:
void millisleep(int milliseconds);
void millisleep(float milliseconds);

   
#endif  /* _SIGTIMER_H_INCLUDED */



// md5sum:	601fa3caae4e3bacc4e6fb87f545c86b  - SigTimer.h =css= 20030102