Jamoma API  0.6.0.a19
AUTimestampGenerator.h
1 /* Copyright © 2007 Apple Inc. All Rights Reserved.
2 
3  Disclaimer: IMPORTANT: This Apple software is supplied to you by
4  Apple Inc. ("Apple") in consideration of your agreement to the
5  following terms, and your use, installation, modification or
6  redistribution of this Apple software constitutes acceptance of these
7  terms. If you do not agree with these terms, please do not use,
8  install, modify or redistribute this Apple software.
9 
10  In consideration of your agreement to abide by the following terms, and
11  subject to these terms, Apple grants you a personal, non-exclusive
12  license, under Apple's copyrights in this original Apple software (the
13  "Apple Software"), to use, reproduce, modify and redistribute the Apple
14  Software, with or without modifications, in source and/or binary forms;
15  provided that if you redistribute the Apple Software in its entirety and
16  without modifications, you must retain this notice and the following
17  text and disclaimers in all such redistributions of the Apple Software.
18  Neither the name, trademarks, service marks or logos of Apple Inc.
19  may be used to endorse or promote products derived from the Apple
20  Software without specific prior written permission from Apple. Except
21  as expressly stated in this notice, no other rights or licenses, express
22  or implied, are granted by Apple herein, including but not limited to
23  any patent rights that may be infringed by your derivative works or by
24  other works in which the Apple Software may be incorporated.
25 
26  The Apple Software is provided by Apple on an "AS IS" basis. APPLE
27  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
28  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
29  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
30  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
31 
32  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
33  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
36  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
37  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
38  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
39  POSSIBILITY OF SUCH DAMAGE.
40 */
41 #ifndef __AUTimestampGenerator_h__
42 #define __AUTimestampGenerator_h__
43 
44 #include <math.h>
45 #include "CAHostTimeBase.h"
46 #include <stdio.h>
47 
48 #define TSGFMT "0x%10qx"
49 //#define TSGFMT "%10qd"
50 
51 // This class generates a continuously increasing series of timestamps based
52 // on a series of potentially discontinuous timestamps (as can be delivered from
53 // CoreAudio in the event of an overload or major engine change).
54 // N.B.: "output" = downstream (source) timestamp
55 // "input" = upstream (derived) timestamp
56 class AUTimestampGenerator {
57 public:
58  AUTimestampGenerator(bool hostTimeDiscontinuityCorrection = false) :
59  mStartInputAtZero(true),
60  mBypassed(false),
61  mHostTimeDiscontinuityCorrection(hostTimeDiscontinuityCorrection)
62  {
63 #if DEBUG
64  mVerbosity = 0;
65  sprintf(mDebugName, "tsg @ %p", this);
66 #endif
67  // CAHostTimeBase should be used instead of the calls in <CoreAudio/HostTime.h>
68  // we make this call here to ensure that this is initialized, otherwise the first time
69  // you do actually call CAHostTimeBase to do work, can be on the render thread, and lead to unwanted VM faults
70  CAHostTimeBase::GetFrequency();
71  Reset();
72  }
73 
74  void SetStartInputAtZero(bool b) { mStartInputAtZero = b; }
75  bool GetStartInputAtZero() const { return mStartInputAtZero; }
76 
77  // bypassing is intended for a narrow special case. the upstream sample time will always be the same as the downstream time.
78  void SetBypassed(bool b) { mBypassed = b; }
79  bool GetBypassed() const { return mBypassed; }
80 
81  // Call this to reset the timeline.
82  void Reset()
83  {
84  mCurrentInputTime.mSampleTime = 0.;
85  mNextInputSampleTime = 0.;
86  mCurrentOutputTime.mSampleTime = 0.;
87  mNextOutputSampleTime = 0.;
88  mLastOutputTime.mFlags = 0;
89  mRateScalarAdj = 1.;
90  mFirstTime = true;
91 #if DEBUG
92  if (mVerbosity)
93  printf("%-20.20s: Reset\n", mDebugName);
94 #endif
95  }
96 
97  // Call this once per render cycle with the downstream timestamp.
98  // expectedDeltaFrames is the expected difference between the current and NEXT
99  // downstream timestamps.
100  // sampleRate is the OUTPUT sample rate.
101  void AddOutputTime(const AudioTimeStamp &inTimeStamp, Float64 expectedDeltaFrames, double outputSampleRate, double rateScalarAdj=1.0);
102 
103  // Call this once per render cycle to obtain the upstream timestamp.
104  // framesToAdvance is the number of frames the input timeline is to be
105  // advanced during this render cycle.
106  // sampleRate is the INPUT sample rate.
107  const AudioTimeStamp & GenerateInputTime(Float64 framesToAdvance, double inputSampleRate);
108 
109  // this can be called to override the setting of the next input sample time in GenerateInputTime
110  void Advance(Float64 framesToAdvance)
111  {
112 #if DEBUG
113  if (mVerbosity > 1)
114  printf("%-20.20s: ADVANCE in = "TSGFMT" advance = "TSGFMT"\n", mDebugName, (SInt64)mCurrentInputTime.mSampleTime, (SInt64)framesToAdvance);
115 #endif
116  mNextInputSampleTime = mCurrentInputTime.mSampleTime + framesToAdvance;
117  }
118 
119 
120 private:
121  AudioTimeStamp mCurrentInputTime;
122  Float64 mNextInputSampleTime;
123  Float64 mNextOutputSampleTime;
124 
125  AudioTimeStamp mLastOutputTime;
126  AudioTimeStamp mCurrentOutputTime;
127 
128  bool mFirstTime;
129  bool mStartInputAtZero; // if true, input timeline starts at 0, else it starts
130  // synced with the output timeline
131  bool mDiscontinuous;
132  bool mBypassed;
133  Float64 mDiscontinuityDeltaSamples;
134 
135  double mRateScalarAdj;
136 
137  bool mHostTimeDiscontinuityCorrection; // If true, propagate timestamp discontinuities using host time.
138 
139 
140 #if DEBUG
141 public:
142  int mVerbosity;
143  char mDebugName[64];
144 #endif
145 };
146 
147 
148 #endif // __AUTimestampGenerator_h__