content/media/AudioEventTimeline.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/media/AudioEventTimeline.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,577 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef AudioEventTimeline_h_
    1.11 +#define AudioEventTimeline_h_
    1.12 +
    1.13 +#include <algorithm>
    1.14 +#include "mozilla/Assertions.h"
    1.15 +#include "mozilla/FloatingPoint.h"
    1.16 +#include "mozilla/TypedEnum.h"
    1.17 +#include "mozilla/PodOperations.h"
    1.18 +
    1.19 +#include "nsTArray.h"
    1.20 +#include "math.h"
    1.21 +
    1.22 +namespace mozilla {
    1.23 +
    1.24 +namespace dom {
    1.25 +
    1.26 +// This is an internal helper class and should not be used outside of this header.
    1.27 +struct AudioTimelineEvent {
    1.28 +  enum Type MOZ_ENUM_TYPE(uint32_t) {
    1.29 +    SetValue,
    1.30 +    LinearRamp,
    1.31 +    ExponentialRamp,
    1.32 +    SetTarget,
    1.33 +    SetValueCurve
    1.34 +  };
    1.35 +
    1.36 +  AudioTimelineEvent(Type aType, double aTime, float aValue, double aTimeConstant = 0.0,
    1.37 +                     float aDuration = 0.0, const float* aCurve = nullptr, uint32_t aCurveLength = 0)
    1.38 +    : mType(aType)
    1.39 +    , mTimeConstant(aTimeConstant)
    1.40 +    , mDuration(aDuration)
    1.41 +#ifdef DEBUG
    1.42 +    , mTimeIsInTicks(false)
    1.43 +#endif
    1.44 +  {
    1.45 +    mTime = aTime;
    1.46 +    if (aType == AudioTimelineEvent::SetValueCurve) {
    1.47 +      SetCurveParams(aCurve, aCurveLength);
    1.48 +    } else {
    1.49 +      mValue = aValue;
    1.50 +    }
    1.51 +  }
    1.52 +
    1.53 +  AudioTimelineEvent(const AudioTimelineEvent& rhs)
    1.54 +  {
    1.55 +    PodCopy(this, &rhs, 1);
    1.56 +    if (rhs.mType == AudioTimelineEvent::SetValueCurve) {
    1.57 +      SetCurveParams(rhs.mCurve, rhs.mCurveLength);
    1.58 +    }
    1.59 +  }
    1.60 +
    1.61 +  ~AudioTimelineEvent()
    1.62 +  {
    1.63 +    if (mType == AudioTimelineEvent::SetValueCurve) {
    1.64 +      delete[] mCurve;
    1.65 +    }
    1.66 +  }
    1.67 +
    1.68 +  bool IsValid() const
    1.69 +  {
    1.70 +    if (mType == AudioTimelineEvent::SetValueCurve) {
    1.71 +      if (!mCurve || !mCurveLength) {
    1.72 +        return false;
    1.73 +      }
    1.74 +      for (uint32_t i = 0; i < mCurveLength; ++i) {
    1.75 +        if (!IsValid(mCurve[i])) {
    1.76 +          return false;
    1.77 +        }
    1.78 +      }
    1.79 +    }
    1.80 +
    1.81 +    return IsValid(mTime) &&
    1.82 +           IsValid(mValue) &&
    1.83 +           IsValid(mTimeConstant) &&
    1.84 +           IsValid(mDuration);
    1.85 +  }
    1.86 +
    1.87 +  template <class TimeType>
    1.88 +  TimeType Time() const;
    1.89 +
    1.90 +  void SetTimeInTicks(int64_t aTimeInTicks)
    1.91 +  {
    1.92 +    mTimeInTicks = aTimeInTicks;
    1.93 +#ifdef DEBUG
    1.94 +    mTimeIsInTicks = true;
    1.95 +#endif
    1.96 +  }
    1.97 +
    1.98 +  void SetCurveParams(const float* aCurve, uint32_t aCurveLength) {
    1.99 +    mCurveLength = aCurveLength;
   1.100 +    if (aCurveLength) {
   1.101 +      mCurve = new float[aCurveLength];
   1.102 +      PodCopy(mCurve, aCurve, aCurveLength);
   1.103 +    } else {
   1.104 +      mCurve = nullptr;
   1.105 +    }
   1.106 +  }
   1.107 +
   1.108 +  Type mType;
   1.109 +  union {
   1.110 +    float mValue;
   1.111 +    uint32_t mCurveLength;
   1.112 +  };
   1.113 +  // The time for an event can either be in absolute value or in ticks.
   1.114 +  // Initially the time of the event is always in absolute value.
   1.115 +  // In order to convert it to ticks, call SetTimeInTicks.  Once this
   1.116 +  // method has been called for an event, the time cannot be converted
   1.117 +  // back to absolute value.
   1.118 +  union {
   1.119 +    double mTime;
   1.120 +    int64_t mTimeInTicks;
   1.121 +  };
   1.122 +  // mCurve contains a buffer of SetValueCurve samples.  We sample the
   1.123 +  // values in the buffer depending on how far along we are in time.
   1.124 +  // If we're at time T and the event has started as time T0 and has a
   1.125 +  // duration of D, we sample the buffer at floor(mCurveLength*(T-T0)/D)
   1.126 +  // if T<T0+D, and just take the last sample in the buffer otherwise.
   1.127 +  float* mCurve;
   1.128 +  double mTimeConstant;
   1.129 +  double mDuration;
   1.130 +#ifdef DEBUG
   1.131 +  bool mTimeIsInTicks;
   1.132 +#endif
   1.133 +
   1.134 +private:
   1.135 +  static bool IsValid(double value)
   1.136 +  {
   1.137 +    return mozilla::IsFinite(value);
   1.138 +  }
   1.139 +};
   1.140 +
   1.141 +template <>
   1.142 +inline double AudioTimelineEvent::Time<double>() const
   1.143 +{
   1.144 +  MOZ_ASSERT(!mTimeIsInTicks);
   1.145 +  return mTime;
   1.146 +}
   1.147 +
   1.148 +template <>
   1.149 +inline int64_t AudioTimelineEvent::Time<int64_t>() const
   1.150 +{
   1.151 +  MOZ_ASSERT(mTimeIsInTicks);
   1.152 +  return mTimeInTicks;
   1.153 +}
   1.154 +
   1.155 +/**
   1.156 + * This class will be instantiated with different template arguments for testing and
   1.157 + * production code.
   1.158 + *
   1.159 + * ErrorResult is a type which satisfies the following:
   1.160 + *  - Implements a Throw() method taking an nsresult argument, representing an error code.
   1.161 + */
   1.162 +template <class ErrorResult>
   1.163 +class AudioEventTimeline
   1.164 +{
   1.165 +public:
   1.166 +  explicit AudioEventTimeline(float aDefaultValue)
   1.167 +    : mValue(aDefaultValue),
   1.168 +      mComputedValue(aDefaultValue),
   1.169 +      mLastComputedValue(aDefaultValue)
   1.170 +  {
   1.171 +  }
   1.172 +
   1.173 +  bool HasSimpleValue() const
   1.174 +  {
   1.175 +    return mEvents.IsEmpty();
   1.176 +  }
   1.177 +
   1.178 +  float GetValue() const
   1.179 +  {
   1.180 +    // This method should only be called if HasSimpleValue() returns true
   1.181 +    MOZ_ASSERT(HasSimpleValue());
   1.182 +    return mValue;
   1.183 +  }
   1.184 +
   1.185 +  float Value() const
   1.186 +  {
   1.187 +    // TODO: Return the current value based on the timeline of the AudioContext
   1.188 +    return mValue;
   1.189 +  }
   1.190 +
   1.191 +  void SetValue(float aValue)
   1.192 +  {
   1.193 +    // Silently don't change anything if there are any events
   1.194 +    if (mEvents.IsEmpty()) {
   1.195 +      mLastComputedValue = mComputedValue = mValue = aValue;
   1.196 +    }
   1.197 +  }
   1.198 +
   1.199 +  void SetValueAtTime(float aValue, double aStartTime, ErrorResult& aRv)
   1.200 +  {
   1.201 +    InsertEvent(AudioTimelineEvent(AudioTimelineEvent::SetValue, aStartTime, aValue), aRv);
   1.202 +  }
   1.203 +
   1.204 +  void LinearRampToValueAtTime(float aValue, double aEndTime, ErrorResult& aRv)
   1.205 +  {
   1.206 +    InsertEvent(AudioTimelineEvent(AudioTimelineEvent::LinearRamp, aEndTime, aValue), aRv);
   1.207 +  }
   1.208 +
   1.209 +  void ExponentialRampToValueAtTime(float aValue, double aEndTime, ErrorResult& aRv)
   1.210 +  {
   1.211 +    InsertEvent(AudioTimelineEvent(AudioTimelineEvent::ExponentialRamp, aEndTime, aValue), aRv);
   1.212 +  }
   1.213 +
   1.214 +  void SetTargetAtTime(float aTarget, double aStartTime, double aTimeConstant, ErrorResult& aRv)
   1.215 +  {
   1.216 +    InsertEvent(AudioTimelineEvent(AudioTimelineEvent::SetTarget, aStartTime, aTarget, aTimeConstant), aRv);
   1.217 +  }
   1.218 +
   1.219 +  void SetValueCurveAtTime(const float* aValues, uint32_t aValuesLength, double aStartTime, double aDuration, ErrorResult& aRv)
   1.220 +  {
   1.221 +    InsertEvent(AudioTimelineEvent(AudioTimelineEvent::SetValueCurve, aStartTime, 0.0f, 0.0f, aDuration, aValues, aValuesLength), aRv);
   1.222 +  }
   1.223 +
   1.224 +  void CancelScheduledValues(double aStartTime)
   1.225 +  {
   1.226 +    for (unsigned i = 0; i < mEvents.Length(); ++i) {
   1.227 +      if (mEvents[i].mTime >= aStartTime) {
   1.228 +#ifdef DEBUG
   1.229 +        // Sanity check: the array should be sorted, so all of the following
   1.230 +        // events should have a time greater than aStartTime too.
   1.231 +        for (unsigned j = i + 1; j < mEvents.Length(); ++j) {
   1.232 +          MOZ_ASSERT(mEvents[j].mTime >= aStartTime);
   1.233 +        }
   1.234 +#endif
   1.235 +        mEvents.TruncateLength(i);
   1.236 +        break;
   1.237 +      }
   1.238 +    }
   1.239 +  }
   1.240 +
   1.241 +  void CancelAllEvents()
   1.242 +  {
   1.243 +    mEvents.Clear();
   1.244 +  }
   1.245 +
   1.246 +  static bool TimesEqual(int64_t aLhs, int64_t aRhs)
   1.247 +  {
   1.248 +    return aLhs == aRhs;
   1.249 +  }
   1.250 +
   1.251 +  // Since we are going to accumulate error by adding 0.01 multiple time in a
   1.252 +  // loop, we want to fuzz the equality check in GetValueAtTime.
   1.253 +  static bool TimesEqual(double aLhs, double aRhs)
   1.254 +  {
   1.255 +    const float kEpsilon = 0.0000000001f;
   1.256 +    return fabs(aLhs - aRhs) < kEpsilon;
   1.257 +  }
   1.258 +
   1.259 +  template<class TimeType>
   1.260 +  float GetValueAtTime(TimeType aTime)
   1.261 +  {
   1.262 +    mComputedValue = GetValueAtTimeHelper(aTime);
   1.263 +    return mComputedValue;
   1.264 +  }
   1.265 +
   1.266 +  // This method computes the AudioParam value at a given time based on the event timeline
   1.267 +  template<class TimeType>
   1.268 +  float GetValueAtTimeHelper(TimeType aTime)
   1.269 +  {
   1.270 +    const AudioTimelineEvent* previous = nullptr;
   1.271 +    const AudioTimelineEvent* next = nullptr;
   1.272 +
   1.273 +    bool bailOut = false;
   1.274 +    for (unsigned i = 0; !bailOut && i < mEvents.Length(); ++i) {
   1.275 +      switch (mEvents[i].mType) {
   1.276 +      case AudioTimelineEvent::SetValue:
   1.277 +      case AudioTimelineEvent::SetTarget:
   1.278 +      case AudioTimelineEvent::LinearRamp:
   1.279 +      case AudioTimelineEvent::ExponentialRamp:
   1.280 +      case AudioTimelineEvent::SetValueCurve:
   1.281 +        if (TimesEqual(aTime, mEvents[i].template Time<TimeType>())) {
   1.282 +          mLastComputedValue = mComputedValue;
   1.283 +          // Find the last event with the same time
   1.284 +          do {
   1.285 +            ++i;
   1.286 +          } while (i < mEvents.Length() &&
   1.287 +                   aTime == mEvents[i].template Time<TimeType>());
   1.288 +
   1.289 +          // SetTarget nodes can be handled no matter what their next node is (if they have one)
   1.290 +          if (mEvents[i - 1].mType == AudioTimelineEvent::SetTarget) {
   1.291 +            // Follow the curve, without regard to the next event, starting at
   1.292 +            // the last value of the last event.
   1.293 +            return ExponentialApproach(mEvents[i - 1].template Time<TimeType>(),
   1.294 +                                       mLastComputedValue, mEvents[i - 1].mValue,
   1.295 +                                       mEvents[i - 1].mTimeConstant, aTime);
   1.296 +          }
   1.297 +
   1.298 +          // SetValueCurve events can be handled no matter what their event node is (if they have one)
   1.299 +          if (mEvents[i - 1].mType == AudioTimelineEvent::SetValueCurve) {
   1.300 +            return ExtractValueFromCurve(mEvents[i - 1].template Time<TimeType>(),
   1.301 +                                         mEvents[i - 1].mCurve,
   1.302 +                                         mEvents[i - 1].mCurveLength,
   1.303 +                                         mEvents[i - 1].mDuration, aTime);
   1.304 +          }
   1.305 +
   1.306 +          // For other event types
   1.307 +          return mEvents[i - 1].mValue;
   1.308 +        }
   1.309 +        previous = next;
   1.310 +        next = &mEvents[i];
   1.311 +        if (aTime < mEvents[i].template Time<TimeType>()) {
   1.312 +          bailOut = true;
   1.313 +        }
   1.314 +        break;
   1.315 +      default:
   1.316 +        MOZ_ASSERT(false, "unreached");
   1.317 +      }
   1.318 +    }
   1.319 +    // Handle the case where the time is past all of the events
   1.320 +    if (!bailOut) {
   1.321 +      previous = next;
   1.322 +      next = nullptr;
   1.323 +    }
   1.324 +
   1.325 +    // Just return the default value if we did not find anything
   1.326 +    if (!previous && !next) {
   1.327 +      return mValue;
   1.328 +    }
   1.329 +
   1.330 +    // If the requested time is before all of the existing events
   1.331 +    if (!previous) {
   1.332 +      return mValue;
   1.333 +    }
   1.334 +
   1.335 +    // SetTarget nodes can be handled no matter what their next node is (if they have one)
   1.336 +    if (previous->mType == AudioTimelineEvent::SetTarget) {
   1.337 +      return ExponentialApproach(previous->template Time<TimeType>(),
   1.338 +                                 mLastComputedValue, previous->mValue,
   1.339 +                                 previous->mTimeConstant, aTime);
   1.340 +    }
   1.341 +
   1.342 +    // SetValueCurve events can be handled no mattar what their next node is (if they have one)
   1.343 +    if (previous->mType == AudioTimelineEvent::SetValueCurve) {
   1.344 +      return ExtractValueFromCurve(previous->template Time<TimeType>(),
   1.345 +                                   previous->mCurve, previous->mCurveLength,
   1.346 +                                   previous->mDuration, aTime);
   1.347 +    }
   1.348 +
   1.349 +    // If the requested time is after all of the existing events
   1.350 +    if (!next) {
   1.351 +      switch (previous->mType) {
   1.352 +      case AudioTimelineEvent::SetValue:
   1.353 +      case AudioTimelineEvent::LinearRamp:
   1.354 +      case AudioTimelineEvent::ExponentialRamp:
   1.355 +        // The value will be constant after the last event
   1.356 +        return previous->mValue;
   1.357 +      case AudioTimelineEvent::SetValueCurve:
   1.358 +        return ExtractValueFromCurve(previous->template Time<TimeType>(),
   1.359 +                                     previous->mCurve, previous->mCurveLength,
   1.360 +                                     previous->mDuration, aTime);
   1.361 +      case AudioTimelineEvent::SetTarget:
   1.362 +        MOZ_ASSERT(false, "unreached");
   1.363 +      }
   1.364 +      MOZ_ASSERT(false, "unreached");
   1.365 +    }
   1.366 +
   1.367 +    // Finally, handle the case where we have both a previous and a next event
   1.368 +
   1.369 +    // First, handle the case where our range ends up in a ramp event
   1.370 +    switch (next->mType) {
   1.371 +    case AudioTimelineEvent::LinearRamp:
   1.372 +      return LinearInterpolate(previous->template Time<TimeType>(), previous->mValue, next->template Time<TimeType>(), next->mValue, aTime);
   1.373 +    case AudioTimelineEvent::ExponentialRamp:
   1.374 +      return ExponentialInterpolate(previous->template Time<TimeType>(), previous->mValue, next->template Time<TimeType>(), next->mValue, aTime);
   1.375 +    case AudioTimelineEvent::SetValue:
   1.376 +    case AudioTimelineEvent::SetTarget:
   1.377 +    case AudioTimelineEvent::SetValueCurve:
   1.378 +      break;
   1.379 +    }
   1.380 +
   1.381 +    // Now handle all other cases
   1.382 +    switch (previous->mType) {
   1.383 +    case AudioTimelineEvent::SetValue:
   1.384 +    case AudioTimelineEvent::LinearRamp:
   1.385 +    case AudioTimelineEvent::ExponentialRamp:
   1.386 +      // If the next event type is neither linear or exponential ramp, the
   1.387 +      // value is constant.
   1.388 +      return previous->mValue;
   1.389 +    case AudioTimelineEvent::SetValueCurve:
   1.390 +      return ExtractValueFromCurve(previous->template Time<TimeType>(),
   1.391 +                                   previous->mCurve, previous->mCurveLength,
   1.392 +                                   previous->mDuration, aTime);
   1.393 +    case AudioTimelineEvent::SetTarget:
   1.394 +      MOZ_ASSERT(false, "unreached");
   1.395 +    }
   1.396 +
   1.397 +    MOZ_ASSERT(false, "unreached");
   1.398 +    return 0.0f;
   1.399 +  }
   1.400 +
   1.401 +  // Return the number of events scheduled
   1.402 +  uint32_t GetEventCount() const
   1.403 +  {
   1.404 +    return mEvents.Length();
   1.405 +  }
   1.406 +
   1.407 +  static float LinearInterpolate(double t0, float v0, double t1, float v1, double t)
   1.408 +  {
   1.409 +    return v0 + (v1 - v0) * ((t - t0) / (t1 - t0));
   1.410 +  }
   1.411 +
   1.412 +  static float ExponentialInterpolate(double t0, float v0, double t1, float v1, double t)
   1.413 +  {
   1.414 +    return v0 * powf(v1 / v0, (t - t0) / (t1 - t0));
   1.415 +  }
   1.416 +
   1.417 +  static float ExponentialApproach(double t0, double v0, float v1, double timeConstant, double t)
   1.418 +  {
   1.419 +    return v1 + (v0 - v1) * expf(-(t - t0) / timeConstant);
   1.420 +  }
   1.421 +
   1.422 +  static float ExtractValueFromCurve(double startTime, float* aCurve, uint32_t aCurveLength, double duration, double t)
   1.423 +  {
   1.424 +    if (t >= startTime + duration) {
   1.425 +      // After the duration, return the last curve value
   1.426 +      return aCurve[aCurveLength - 1];
   1.427 +    }
   1.428 +    double ratio = std::max((t - startTime) / duration, 0.0);
   1.429 +    if (ratio >= 1.0) {
   1.430 +      return aCurve[aCurveLength - 1];
   1.431 +    }
   1.432 +    return aCurve[uint32_t(aCurveLength * ratio)];
   1.433 +  }
   1.434 +
   1.435 +  void ConvertEventTimesToTicks(int64_t (*aConvertor)(double aTime, void* aClosure), void* aClosure,
   1.436 +                                int32_t aSampleRate)
   1.437 +  {
   1.438 +    for (unsigned i = 0; i < mEvents.Length(); ++i) {
   1.439 +      mEvents[i].SetTimeInTicks(aConvertor(mEvents[i].template Time<double>(), aClosure));
   1.440 +      mEvents[i].mTimeConstant *= aSampleRate;
   1.441 +      mEvents[i].mDuration *= aSampleRate;
   1.442 +    }
   1.443 +  }
   1.444 +
   1.445 +private:
   1.446 +  const AudioTimelineEvent* GetPreviousEvent(double aTime) const
   1.447 +  {
   1.448 +    const AudioTimelineEvent* previous = nullptr;
   1.449 +    const AudioTimelineEvent* next = nullptr;
   1.450 +
   1.451 +    bool bailOut = false;
   1.452 +    for (unsigned i = 0; !bailOut && i < mEvents.Length(); ++i) {
   1.453 +      switch (mEvents[i].mType) {
   1.454 +      case AudioTimelineEvent::SetValue:
   1.455 +      case AudioTimelineEvent::SetTarget:
   1.456 +      case AudioTimelineEvent::LinearRamp:
   1.457 +      case AudioTimelineEvent::ExponentialRamp:
   1.458 +      case AudioTimelineEvent::SetValueCurve:
   1.459 +        if (aTime == mEvents[i].mTime) {
   1.460 +          // Find the last event with the same time
   1.461 +          do {
   1.462 +            ++i;
   1.463 +          } while (i < mEvents.Length() &&
   1.464 +                   aTime == mEvents[i].mTime);
   1.465 +          return &mEvents[i - 1];
   1.466 +        }
   1.467 +        previous = next;
   1.468 +        next = &mEvents[i];
   1.469 +        if (aTime < mEvents[i].mTime) {
   1.470 +          bailOut = true;
   1.471 +        }
   1.472 +        break;
   1.473 +      default:
   1.474 +        MOZ_ASSERT(false, "unreached");
   1.475 +      }
   1.476 +    }
   1.477 +    // Handle the case where the time is past all of the events
   1.478 +    if (!bailOut) {
   1.479 +      previous = next;
   1.480 +    }
   1.481 +
   1.482 +    return previous;
   1.483 +  }
   1.484 +
   1.485 +  void InsertEvent(const AudioTimelineEvent& aEvent, ErrorResult& aRv)
   1.486 +  {
   1.487 +    if (!aEvent.IsValid()) {
   1.488 +      aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
   1.489 +      return;
   1.490 +    }
   1.491 +
   1.492 +    // Make sure that non-curve events don't fall within the duration of a
   1.493 +    // curve event.
   1.494 +    for (unsigned i = 0; i < mEvents.Length(); ++i) {
   1.495 +      if (mEvents[i].mType == AudioTimelineEvent::SetValueCurve &&
   1.496 +          mEvents[i].mTime <= aEvent.mTime &&
   1.497 +          (mEvents[i].mTime + mEvents[i].mDuration) >= aEvent.mTime) {
   1.498 +        aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
   1.499 +        return;
   1.500 +      }
   1.501 +    }
   1.502 +
   1.503 +    // Make sure that curve events don't fall in a range which includes other
   1.504 +    // events.
   1.505 +    if (aEvent.mType == AudioTimelineEvent::SetValueCurve) {
   1.506 +      for (unsigned i = 0; i < mEvents.Length(); ++i) {
   1.507 +        if (mEvents[i].mTime > aEvent.mTime &&
   1.508 +            mEvents[i].mTime < (aEvent.mTime + aEvent.mDuration)) {
   1.509 +          aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
   1.510 +          return;
   1.511 +        }
   1.512 +      }
   1.513 +    }
   1.514 +
   1.515 +    // Make sure that invalid values are not used for exponential curves
   1.516 +    if (aEvent.mType == AudioTimelineEvent::ExponentialRamp) {
   1.517 +      if (aEvent.mValue <= 0.f) {
   1.518 +        aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
   1.519 +        return;
   1.520 +      }
   1.521 +      const AudioTimelineEvent* previousEvent = GetPreviousEvent(aEvent.mTime);
   1.522 +      if (previousEvent) {
   1.523 +        if (previousEvent->mValue <= 0.f) {
   1.524 +          aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
   1.525 +          return;
   1.526 +        }
   1.527 +      } else {
   1.528 +        if (mValue <= 0.f) {
   1.529 +          aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
   1.530 +          return;
   1.531 +        }
   1.532 +      }
   1.533 +    }
   1.534 +
   1.535 +    for (unsigned i = 0; i < mEvents.Length(); ++i) {
   1.536 +      if (aEvent.mTime == mEvents[i].mTime) {
   1.537 +        if (aEvent.mType == mEvents[i].mType) {
   1.538 +          // If times and types are equal, replace the event
   1.539 +          mEvents.ReplaceElementAt(i, aEvent);
   1.540 +        } else {
   1.541 +          // Otherwise, place the element after the last event of another type
   1.542 +          do {
   1.543 +            ++i;
   1.544 +          } while (i < mEvents.Length() &&
   1.545 +                   aEvent.mType != mEvents[i].mType &&
   1.546 +                   aEvent.mTime == mEvents[i].mTime);
   1.547 +          mEvents.InsertElementAt(i, aEvent);
   1.548 +        }
   1.549 +        return;
   1.550 +      }
   1.551 +      // Otherwise, place the event right after the latest existing event
   1.552 +      if (aEvent.mTime < mEvents[i].mTime) {
   1.553 +        mEvents.InsertElementAt(i, aEvent);
   1.554 +        return;
   1.555 +      }
   1.556 +    }
   1.557 +
   1.558 +    // If we couldn't find a place for the event, just append it to the list
   1.559 +    mEvents.AppendElement(aEvent);
   1.560 +  }
   1.561 +
   1.562 +private:
   1.563 +  // This is a sorted array of the events in the timeline.  Queries of this
   1.564 +  // data structure should probably be more frequent than modifications to it,
   1.565 +  // and that is the reason why we're using a simple array as the data structure.
   1.566 +  // We can optimize this in the future if the performance of the array ends up
   1.567 +  // being a bottleneck.
   1.568 +  nsTArray<AudioTimelineEvent> mEvents;
   1.569 +  float mValue;
   1.570 +  // This is the value of this AudioParam we computed at the last call.
   1.571 +  float mComputedValue;
   1.572 +  // This is the value of this AudioParam at the last tick of the previous event.
   1.573 +  float mLastComputedValue;
   1.574 +};
   1.575 +
   1.576 +}
   1.577 +}
   1.578 +
   1.579 +#endif
   1.580 +

mercurial