| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 double eventFrame = event.time() * sampleRate; | 398 double eventFrame = event.time() * sampleRate; |
| 399 | 399 |
| 400 // Condition is currentFrame - 1 < eventFrame <= currentFrame, but c
urrentFrame is | 400 // Condition is currentFrame - 1 < eventFrame <= currentFrame, but c
urrentFrame is |
| 401 // unsigned and could be 0, so use currentFrame < eventFrame + 1 ins
tead. | 401 // unsigned and could be 0, so use currentFrame < eventFrame + 1 ins
tead. |
| 402 if (!((event.type() == ParamEvent::SetValue | 402 if (!((event.type() == ParamEvent::SetValue |
| 403 && (eventFrame <= currentFrame) | 403 && (eventFrame <= currentFrame) |
| 404 && (currentFrame < eventFrame + 1)))) | 404 && (currentFrame < eventFrame + 1)))) |
| 405 continue; | 405 continue; |
| 406 } | 406 } |
| 407 | 407 |
| 408 ParamEvent::Type nextEventType = nextEvent ? static_cast<ParamEvent::Typ
e>(nextEvent->type()) : ParamEvent::LastType /* unknown */; |
| 409 |
| 410 // If the current event is SetTarget and the next event is a LinearRampT
oValue or |
| 411 // ExponentialRampToValue, special handling is needed. In this case, th
e linear and |
| 412 // exponential ramp should start at wherever the SetTarget processing ha
s reached. |
| 413 if (event.type() == ParamEvent::SetTarget |
| 414 && (nextEventType == ParamEvent::LinearRampToValue |
| 415 || nextEventType == ParamEvent::ExponentialRampToValue)) { |
| 416 // Replace the SetTarget with a SetValue to set the starting time an
d value for the ramp |
| 417 // using the current frame and value. |
| 418 m_events[i] = ParamEvent::createSetValueEvent(value, currentFrame /
sampleRate); |
| 419 } |
| 420 |
| 408 float value1 = event.value(); | 421 float value1 = event.value(); |
| 409 double time1 = event.time(); | 422 double time1 = event.time(); |
| 410 | 423 |
| 411 float value2 = nextEvent ? nextEvent->value() : value1; | 424 float value2 = nextEvent ? nextEvent->value() : value1; |
| 412 double time2 = nextEvent ? nextEvent->time() : endFrame / sampleRate + 1
; | 425 double time2 = nextEvent ? nextEvent->time() : endFrame / sampleRate + 1
; |
| 413 | 426 |
| 414 double deltaTime = time2 - time1; | 427 double deltaTime = time2 - time1; |
| 415 float k = deltaTime > 0 ? 1 / deltaTime : 0; | 428 float k = deltaTime > 0 ? 1 / deltaTime : 0; |
| 416 | 429 |
| 417 // |fillToEndFrame| is the exclusive upper bound of the last frame to be
computed for this | 430 // |fillToEndFrame| is the exclusive upper bound of the last frame to be
computed for this |
| 418 // event. It's either the last desired frame (|endFrame|) or derived fr
om the end time of | 431 // event. It's either the last desired frame (|endFrame|) or derived fr
om the end time of |
| 419 // the next event (time2). We compute ceil(time2*sampleRate) because fil
lToEndFrame is the | 432 // the next event (time2). We compute ceil(time2*sampleRate) because fil
lToEndFrame is the |
| 420 // exclusive upper bound. Consider the case where |startFrame| = 128 an
d time2 = 128.1 | 433 // exclusive upper bound. Consider the case where |startFrame| = 128 an
d time2 = 128.1 |
| 421 // (assuming sampleRate = 1). Since time2 is greater than 128, we want
to output a value | 434 // (assuming sampleRate = 1). Since time2 is greater than 128, we want
to output a value |
| 422 // for frame 128. This requires that fillToEndFrame be at least 129. T
his is achieved by | 435 // for frame 128. This requires that fillToEndFrame be at least 129. T
his is achieved by |
| 423 // ceil(time2). | 436 // ceil(time2). |
| 424 size_t fillToEndFrame = std::min(endFrame, static_cast<size_t>(ceil(time
2 * sampleRate))); | 437 size_t fillToEndFrame = std::min(endFrame, static_cast<size_t>(ceil(time
2 * sampleRate))); |
| 425 ASSERT(fillToEndFrame >= startFrame); | 438 ASSERT(fillToEndFrame >= startFrame); |
| 426 size_t fillToFrame = fillToEndFrame - startFrame; | 439 size_t fillToFrame = fillToEndFrame - startFrame; |
| 427 fillToFrame = std::min(fillToFrame, static_cast<size_t>(numberOfValues))
; | 440 fillToFrame = std::min(fillToFrame, static_cast<size_t>(numberOfValues))
; |
| 428 | 441 |
| 429 ParamEvent::Type nextEventType = nextEvent ? static_cast<ParamEvent::Typ
e>(nextEvent->type()) : ParamEvent::LastType /* unknown */; | |
| 430 | 442 |
| 431 // First handle linear and exponential ramps which require looking ahead
to the next event. | 443 // First handle linear and exponential ramps which require looking ahead
to the next event. |
| 432 if (nextEventType == ParamEvent::LinearRampToValue) { | 444 if (nextEventType == ParamEvent::LinearRampToValue) { |
| 433 const float valueDelta = value2 - value1; | 445 const float valueDelta = value2 - value1; |
| 434 #if CPU(X86) || CPU(X86_64) | 446 #if CPU(X86) || CPU(X86_64) |
| 435 // Minimize in-loop operations. Calculate starting value and increme
nt. Next step: value += inc. | 447 // Minimize in-loop operations. Calculate starting value and increme
nt. Next step: value += inc. |
| 436 // value = value1 + (currentFrame/sampleRate - time1) * k * (value2
- value1); | 448 // value = value1 + (currentFrame/sampleRate - time1) * k * (value2
- value1); |
| 437 // inc = 4 / sampleRate * k * (value2 - value1); | 449 // inc = 4 / sampleRate * k * (value2 - value1); |
| 438 // Resolve recursion by expanding constants to achieve a 4-step loop
unrolling. | 450 // Resolve recursion by expanding constants to achieve a 4-step loop
unrolling. |
| 439 // value = value1 + ((currentFrame/sampleRate - time1) + i * sample
FrameTimeIncr) * k * (value2 -value1), i in 0..3 | 451 // value = value1 + ((currentFrame/sampleRate - time1) + i * sample
FrameTimeIncr) * k * (value2 -value1), i in 0..3 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 float multiplier = powf(value2 / value1, 1 / numSampleFrames); | 515 float multiplier = powf(value2 / value1, 1 / numSampleFrames); |
| 504 // Set the starting value of the exponential ramp. | 516 // Set the starting value of the exponential ramp. |
| 505 value = value1 * powf(value2 / value1, | 517 value = value1 * powf(value2 / value1, |
| 506 (currentFrame / sampleRate - time1) / deltaTime); | 518 (currentFrame / sampleRate - time1) / deltaTime); |
| 507 | 519 |
| 508 for (; writeIndex < fillToFrame; ++writeIndex) { | 520 for (; writeIndex < fillToFrame; ++writeIndex) { |
| 509 values[writeIndex] = value; | 521 values[writeIndex] = value; |
| 510 value *= multiplier; | 522 value *= multiplier; |
| 511 ++currentFrame; | 523 ++currentFrame; |
| 512 } | 524 } |
| 525 // Due to roundoff it's possible that value exceeds value2. Cli
p value to value2 if |
| 526 // we are within 1/2 frame of time2. |
| 527 if (currentFrame > time2 * sampleRate - 0.5) |
| 528 value = value2; |
| 513 } | 529 } |
| 514 } else { | 530 } else { |
| 515 // Handle event types not requiring looking ahead to the next event. | 531 // Handle event types not requiring looking ahead to the next event. |
| 516 switch (event.type()) { | 532 switch (event.type()) { |
| 517 case ParamEvent::SetValue: | 533 case ParamEvent::SetValue: |
| 518 case ParamEvent::LinearRampToValue: | 534 case ParamEvent::LinearRampToValue: |
| 519 { | 535 { |
| 520 currentFrame = fillToEndFrame; | 536 currentFrame = fillToEndFrame; |
| 521 | 537 |
| 522 // Simply stay at a constant value. | 538 // Simply stay at a constant value. |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 764 // to the end of the values buffer. | 780 // to the end of the values buffer. |
| 765 for (; writeIndex < numberOfValues; ++writeIndex) | 781 for (; writeIndex < numberOfValues; ++writeIndex) |
| 766 values[writeIndex] = value; | 782 values[writeIndex] = value; |
| 767 | 783 |
| 768 // This value is used to set the .value attribute of the AudioParam. | 784 // This value is used to set the .value attribute of the AudioParam. |
| 769 return value; | 785 return value; |
| 770 } | 786 } |
| 771 | 787 |
| 772 } // namespace blink | 788 } // namespace blink |
| 773 | 789 |
| OLD | NEW |