Jamoma API  0.6.0.a19
AUTimestampGenerator.cpp
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 #include "AUTimestampGenerator.h"
42 #include "CAMath.h"
43 
44 #if DEBUG
45 static double DebugHostTime(const AudioTimeStamp &ts)
46 {
47  static UInt64 baseHostTime = 0;
48  if (!(ts.mFlags & kAudioTimeStampHostTimeValid))
49  return -1.;
50  if (baseHostTime == 0)
51  baseHostTime = ts.mHostTime;
52  return double(SInt64(ts.mHostTime) - SInt64(baseHostTime)) * CAHostTimeBase::GetInverseFrequency();
53 }
54 #endif
55 
56 void AUTimestampGenerator::AddOutputTime(const AudioTimeStamp &inTimeStamp, Float64 expectedDeltaFrames, double outputSampleRate, double rateScalarAdj)
57 {
58  mRateScalarAdj = rateScalarAdj;
59  mLastOutputTime = mCurrentOutputTime;
60  mCurrentOutputTime = inTimeStamp;
61  if (mBypassed)
62  return;
63  if (mHostTimeDiscontinuityCorrection && !(mCurrentOutputTime.mFlags & kAudioTimeStampHostTimeValid) && (mLastOutputTime.mFlags & kAudioTimeStampHostTimeValid)) {
64  // no host time here but we had one last time, interpolate one
65  double rateScalar = (mCurrentOutputTime.mFlags & kAudioTimeStampRateScalarValid) ? mCurrentOutputTime.mRateScalar : 1.0;
66  Float64 deltaSamples = mCurrentOutputTime.mSampleTime - mLastOutputTime.mSampleTime;
67  mCurrentOutputTime.mHostTime = mLastOutputTime.mHostTime +
68  UInt64(CAHostTimeBase::GetFrequency() * deltaSamples * rateScalar / outputSampleRate);
69  mCurrentOutputTime.mFlags |= kAudioTimeStampHostTimeValid;
70 #if DEBUG
71  if (mVerbosity > 1)
72  printf("synthesized host time: %.3f (%.3f + %.f smp @ %.f Hz, rs %.3f\n", DebugHostTime(mCurrentOutputTime), DebugHostTime(mLastOutputTime), deltaSamples, outputSampleRate, rateScalar);
73 #endif
74  }
75  // copy rate scalar
76  if (rateScalarAdj != 1.0) {
77  if (mCurrentOutputTime.mFlags & kAudioTimeStampRateScalarValid)
78  mCurrentOutputTime.mRateScalar *= rateScalarAdj;
79  else {
80  mCurrentOutputTime.mRateScalar = rateScalarAdj;
81  mCurrentOutputTime.mFlags |= kAudioTimeStampRateScalarValid;
82  }
83  }
84 
85  if (mFirstTime) {
86  mFirstTime = false;
87  mDiscontinuous = false;
88  mDiscontinuityDeltaSamples = 0.;
89  if (!mStartInputAtZero)
90  mNextInputSampleTime = mCurrentOutputTime.mSampleTime;
91  } else {
92  mDiscontinuous = fnotequal(mCurrentOutputTime.mSampleTime, mNextOutputSampleTime);
93  mDiscontinuityDeltaSamples = mCurrentOutputTime.mSampleTime - mNextOutputSampleTime;
94  // time should never go backwards...
95  if (mDiscontinuityDeltaSamples < 0.)
96  mDiscontinuityDeltaSamples = 0.;
97 #if DEBUG
98  if (mVerbosity > 1)
99  if (mDiscontinuous)
100  printf("%-20.20s: *** DISCONTINUOUS, got "TSGFMT", expected "TSGFMT"\n", mDebugName, (SInt64)mCurrentOutputTime.mSampleTime, (SInt64)mNextOutputSampleTime);
101 #endif
102  }
103  mNextOutputSampleTime = mCurrentOutputTime.mSampleTime + expectedDeltaFrames;
104 }
105 
106 const AudioTimeStamp & AUTimestampGenerator::GenerateInputTime(Float64 framesToAdvance, double inputSampleRate)
107 {
108  if (mBypassed)
109  return mCurrentOutputTime;
110 
111  double inputSampleTime;
112 
113  mCurrentInputTime.mFlags = kAudioTimeStampSampleTimeValid;
114  double rateScalar = 1.0;
115 
116  // propagate rate scalar
117  if (mCurrentOutputTime.mFlags & kAudioTimeStampRateScalarValid) {
118  mCurrentInputTime.mFlags |= kAudioTimeStampRateScalarValid;
119  mCurrentInputTime.mRateScalar = rateScalar = mCurrentOutputTime.mRateScalar;
120  }
121 
122  // propagate host time and sample time
123  if (mCurrentOutputTime.mFlags & kAudioTimeStampHostTimeValid) {
124  mCurrentInputTime.mFlags |= kAudioTimeStampHostTimeValid;
125  mCurrentInputTime.mHostTime = mCurrentOutputTime.mHostTime;
126  if (mHostTimeDiscontinuityCorrection && mDiscontinuous && (mLastOutputTime.mFlags & kAudioTimeStampHostTimeValid)) {
127  // we had a discontinuous output time, need to resync by interpolating
128  // a sample time that is appropriate to the host time
129  UInt64 deltaHostTime = mCurrentOutputTime.mHostTime - mLastOutputTime.mHostTime;
130  double deltaSeconds = double(deltaHostTime) * CAHostTimeBase::GetInverseFrequency();
131  // samples/second * seconds = samples
132  double deltaSamples = floor(inputSampleRate / rateScalar * deltaSeconds + 0.5);
133  double lastInputSampleTime = mCurrentInputTime.mSampleTime;
134  inputSampleTime = lastInputSampleTime + deltaSamples;
135 #if DEBUG
136  if (mVerbosity > 1)
137  printf("%-20.20s: adjusted input time: "TSGFMT" -> "TSGFMT" (SR=%.3f, rs=%.3f)\n", mDebugName, (SInt64)lastInputSampleTime, (SInt64)inputSampleTime, inputSampleRate, rateScalar);
138 #endif
139  mDiscontinuous = false;
140  } else {
141  inputSampleTime = mNextInputSampleTime;
142  }
143  } else {
144  // we don't know the host time, so we can't do much
145  inputSampleTime = mNextInputSampleTime;
146  }
147 
148  if (!mHostTimeDiscontinuityCorrection && fnonzero(mDiscontinuityDeltaSamples))
149  {
150  // we had a discontinuous output time, need to resync by propagating the
151  // detected discontinuity, taking the rate scalar adjustment into account
152  inputSampleTime += floor(mDiscontinuityDeltaSamples / mRateScalarAdj + 0.5);
153 
154 #if DEBUG
155  if (mVerbosity > 1)
156  printf("%-20.20s: adjusted input time: %.0f -> %.0f (SR=%.3f, rs=%.3f, delta=%.0f)\n", mDebugName, mNextInputSampleTime, inputSampleTime, inputSampleRate, mRateScalarAdj, mDiscontinuityDeltaSamples);
157 #endif
158 
159  mDiscontinuityDeltaSamples = 0.;
160  }
161 
162 
163  // propagate word clock
164  if (mCurrentOutputTime.mFlags & kAudioTimeStampWordClockTimeValid) {
165  mCurrentInputTime.mFlags |= kAudioTimeStampWordClockTimeValid;
166  mCurrentInputTime.mWordClockTime = mCurrentOutputTime.mWordClockTime;
167  }
168 
169  // propagate SMPTE time
170  if (mCurrentOutputTime.mFlags & kAudioTimeStampSMPTETimeValid) {
171  mCurrentInputTime.mFlags |= kAudioTimeStampSMPTETimeValid;
172  mCurrentInputTime.mSMPTETime = mCurrentOutputTime.mSMPTETime;
173  }
174 
175  // store the input sample time and expected next input time
176  mCurrentInputTime.mSampleTime = inputSampleTime;
177  mNextInputSampleTime = inputSampleTime + framesToAdvance;
178 
179 #if DEBUG
180  if (mVerbosity > 0) {
181  printf("%-20.20s: out = "TSGFMT" (%10.3fs) in = "TSGFMT" (%10.3fs) delta = "TSGFMT" advance = "TSGFMT"\n", mDebugName, (SInt64)mCurrentOutputTime.mSampleTime, DebugHostTime(mCurrentOutputTime), (SInt64)inputSampleTime, DebugHostTime(mCurrentInputTime), (SInt64)(mCurrentOutputTime.mSampleTime - inputSampleTime), (SInt64)framesToAdvance);
182  }
183 #endif
184  return mCurrentInputTime;
185 }