| 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 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 float AudioParamTimeline::valuesForFrameRangeImpl( | 341 float AudioParamTimeline::valuesForFrameRangeImpl( |
| 342 size_t startFrame, | 342 size_t startFrame, |
| 343 size_t endFrame, | 343 size_t endFrame, |
| 344 float defaultValue, | 344 float defaultValue, |
| 345 float* values, | 345 float* values, |
| 346 unsigned numberOfValues, | 346 unsigned numberOfValues, |
| 347 double sampleRate, | 347 double sampleRate, |
| 348 double controlRate) | 348 double controlRate) |
| 349 { | 349 { |
| 350 ASSERT(values); | 350 ASSERT(values); |
| 351 if (!values) | 351 ASSERT(numberOfValues >= 1); |
| 352 if (!values || !(numberOfValues >= 1)) |
| 352 return defaultValue; | 353 return defaultValue; |
| 353 | 354 |
| 354 // Return default value if there are no events matching the desired time ran
ge. | 355 // Return default value if there are no events matching the desired time ran
ge. |
| 355 if (!m_events.size() || (endFrame / sampleRate <= m_events[0].time())) { | 356 if (!m_events.size() || (endFrame / sampleRate <= m_events[0].time())) { |
| 356 for (unsigned i = 0; i < numberOfValues; ++i) | 357 for (unsigned i = 0; i < numberOfValues; ++i) |
| 357 values[i] = defaultValue; | 358 values[i] = defaultValue; |
| 358 return defaultValue; | 359 return defaultValue; |
| 359 } | 360 } |
| 360 | 361 |
| 361 // Maintain a running time (frame) and index for writing the values buffer. | 362 // Maintain a running time (frame) and index for writing the values buffer. |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 float multiplier = powf(value2 / value1, 1 / numSampleFrames); | 516 float multiplier = powf(value2 / value1, 1 / numSampleFrames); |
| 516 // Set the starting value of the exponential ramp. | 517 // Set the starting value of the exponential ramp. |
| 517 value = value1 * powf(value2 / value1, | 518 value = value1 * powf(value2 / value1, |
| 518 (currentFrame / sampleRate - time1) / deltaTime); | 519 (currentFrame / sampleRate - time1) / deltaTime); |
| 519 | 520 |
| 520 for (; writeIndex < fillToFrame; ++writeIndex) { | 521 for (; writeIndex < fillToFrame; ++writeIndex) { |
| 521 values[writeIndex] = value; | 522 values[writeIndex] = value; |
| 522 value *= multiplier; | 523 value *= multiplier; |
| 523 ++currentFrame; | 524 ++currentFrame; |
| 524 } | 525 } |
| 526 // |value| got updated one extra time in the above loop. Restor
e it to the last |
| 527 // computed value. |
| 528 if (writeIndex >= 1) |
| 529 value /= multiplier; |
| 525 } | 530 } |
| 526 } else { | 531 } else { |
| 527 // Handle event types not requiring looking ahead to the next event. | 532 // Handle event types not requiring looking ahead to the next event. |
| 528 switch (event.type()) { | 533 switch (event.type()) { |
| 529 case ParamEvent::SetValue: | 534 case ParamEvent::SetValue: |
| 530 case ParamEvent::LinearRampToValue: | 535 case ParamEvent::LinearRampToValue: |
| 531 { | 536 { |
| 532 currentFrame = fillToEndFrame; | 537 currentFrame = fillToEndFrame; |
| 533 | 538 |
| 534 // Simply stay at a constant value. | 539 // Simply stay at a constant value. |
| 535 value = event.value(); | 540 value = event.value(); |
| 536 | 541 |
| 537 for (; writeIndex < fillToFrame; ++writeIndex) | 542 for (; writeIndex < fillToFrame; ++writeIndex) |
| 538 values[writeIndex] = value; | 543 values[writeIndex] = value; |
| 539 | 544 |
| 540 break; | 545 break; |
| 541 } | 546 } |
| 542 | 547 |
| 543 case ParamEvent::ExponentialRampToValue: | 548 case ParamEvent::ExponentialRampToValue: |
| 544 { | 549 { |
| 545 currentFrame = fillToEndFrame; | 550 currentFrame = fillToEndFrame; |
| 546 | 551 |
| 552 // If we're here, we've reached the end of the ramp. If we
can (because the |
| 553 // start and end values have the same sign, and neither is 0
), use the actual |
| 554 // end value. If not, we have to propagate whatever we have
. |
| 555 if (i >= 1 && ((m_events[i - 1].value() * event.value()) > 0
)) |
| 556 value = event.value(); |
| 557 |
| 547 // Simply stay at a constant value from the last time. We d
on't want to use the | 558 // Simply stay at a constant value from the last time. We d
on't want to use the |
| 548 // value of the event in case value1 * value2 < 0. In this
case we should | 559 // value of the event in case value1 * value2 < 0. In this
case we should |
| 549 // propagate the previous value, which is in |value|. | 560 // propagate the previous value, which is in |value|. |
| 550 for (; writeIndex < fillToFrame; ++writeIndex) | 561 for (; writeIndex < fillToFrame; ++writeIndex) |
| 551 values[writeIndex] = value; | 562 values[writeIndex] = value; |
| 552 | 563 |
| 553 break; | 564 break; |
| 554 } | 565 } |
| 555 | 566 |
| 556 case ParamEvent::SetTarget: | 567 case ParamEvent::SetTarget: |
| 557 { | 568 { |
| 558 // Exponential approach to target value with given time cons
tant. | 569 // Exponential approach to target value with given time cons
tant. |
| 559 // | 570 // |
| 560 // v(t) = v2 + (v1 - v2)*exp(-(t-t1/tau)) | 571 // v(t) = v2 + (v1 - v2)*exp(-(t-t1/tau)) |
| 561 // | 572 // |
| 562 | 573 |
| 563 float target = event.value(); | 574 float target = event.value(); |
| 564 float timeConstant = event.timeConstant(); | 575 float timeConstant = event.timeConstant(); |
| 565 float discreteTimeConstant = static_cast<float>(AudioUtiliti
es::discreteTimeConstantForSampleRate(timeConstant, controlRate)); | 576 float discreteTimeConstant = static_cast<float>(AudioUtiliti
es::discreteTimeConstantForSampleRate(timeConstant, controlRate)); |
| 566 | 577 |
| 567 // Set the starting value correctly. This is only needed wh
en the current time | 578 // Set the starting value correctly. This is only needed wh
en the current time |
| 568 // is "equal" to the start time of this event. This is to g
et the sampling | 579 // is "equal" to the start time of this event. This is to g
et the sampling |
| 569 // correct if the start time of this automation isn't on a f
rame boundary. | 580 // correct if the start time of this automation isn't on a f
rame boundary. |
| 570 // Otherwise, we can just continue from where we left off fr
om the previous | 581 // Otherwise, we can just continue from where we left off fr
om the previous |
| 571 // rendering quantum. | 582 // rendering quantum. |
| 572 | |
| 573 { | 583 { |
| 574 double rampStartFrame = time1 * sampleRate; | 584 double rampStartFrame = time1 * sampleRate; |
| 575 // Condition is c - 1 < r <= c where c = currentFrame an
d r = | 585 // Condition is c - 1 < r <= c where c = currentFrame an
d r = |
| 576 // rampStartFrame. Compute it this way because currentF
rame is unsigned and | 586 // rampStartFrame. Compute it this way because currentF
rame is unsigned and |
| 577 // could be 0. | 587 // could be 0. |
| 578 if (rampStartFrame <= currentFrame && currentFrame < ram
pStartFrame + 1) | 588 if (rampStartFrame <= currentFrame && currentFrame < ram
pStartFrame + 1) { |
| 579 value = target + (value - target) * exp(-(currentFra
me / sampleRate - time1) / timeConstant); | 589 value = target + (value - target) * exp(-(currentFra
me / sampleRate - time1) / timeConstant); |
| 590 } else { |
| 591 // Otherwise, need to compute a new value bacause |v
alue| is the last |
| 592 // computed value of SetTarget. Time has progressed
by one frame, so we |
| 593 // need to update the value for the new frame. |
| 594 value += (target - value) * discreteTimeConstant; |
| 595 } |
| 580 } | 596 } |
| 581 | 597 |
| 582 // If the value is close enough to the target, just fill in
the data with the | 598 // If the value is close enough to the target, just fill in
the data with the |
| 583 // target value. | 599 // target value. |
| 584 if (fabs(value - target) < kSetTargetThreshold * fabs(target
) | 600 if (fabs(value - target) < kSetTargetThreshold * fabs(target
) |
| 585 || (!target && fabs(value) < kSetTargetZeroThreshold)) { | 601 || (!target && fabs(value) < kSetTargetZeroThreshold)) { |
| 586 for (; writeIndex < fillToFrame; ++writeIndex) | 602 for (; writeIndex < fillToFrame; ++writeIndex) |
| 587 values[writeIndex] = target; | 603 values[writeIndex] = target; |
| 588 } else { | 604 } else { |
| 589 #if CPU(X86) || CPU(X86_64) | 605 #if CPU(X86) || CPU(X86_64) |
| (...skipping 25 matching lines...) Expand all Loading... |
| 615 | 631 |
| 616 // Update value for next iteration. | 632 // Update value for next iteration. |
| 617 value += delta * c3; | 633 value += delta * c3; |
| 618 } | 634 } |
| 619 #endif | 635 #endif |
| 620 // Serially process remaining values | 636 // Serially process remaining values |
| 621 for (; writeIndex < fillToFrame; ++writeIndex) { | 637 for (; writeIndex < fillToFrame; ++writeIndex) { |
| 622 values[writeIndex] = value; | 638 values[writeIndex] = value; |
| 623 value += (target - value) * discreteTimeConstant; | 639 value += (target - value) * discreteTimeConstant; |
| 624 } | 640 } |
| 625 | 641 // The previous loops may have updated |value| one extra
time. Reset it to |
| 642 // the last computed value. |
| 643 if (writeIndex >= 1) |
| 644 value = values[writeIndex - 1]; |
| 626 currentFrame = fillToEndFrame; | 645 currentFrame = fillToEndFrame; |
| 627 } | 646 } |
| 628 break; | 647 break; |
| 629 } | 648 } |
| 630 | 649 |
| 631 case ParamEvent::SetValueCurve: | 650 case ParamEvent::SetValueCurve: |
| 632 { | 651 { |
| 633 DOMFloat32Array* curve = event.curve(); | 652 DOMFloat32Array* curve = event.curve(); |
| 634 float* curveData = curve ? curve->data() : 0; | 653 float* curveData = curve ? curve->data() : 0; |
| 635 unsigned numberOfCurvePoints = curve ? curve->length() : 0; | 654 unsigned numberOfCurvePoints = curve ? curve->length() : 0; |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 758 float c1 = curveData[curveIndex1]; | 777 float c1 = curveData[curveIndex1]; |
| 759 double delta = std::min(currentVirtualIndex - curveIndex
0, 1.0); | 778 double delta = std::min(currentVirtualIndex - curveIndex
0, 1.0); |
| 760 | 779 |
| 761 value = c0 + (c1 - c0) * delta; | 780 value = c0 + (c1 - c0) * delta; |
| 762 | 781 |
| 763 values[writeIndex] = value; | 782 values[writeIndex] = value; |
| 764 } | 783 } |
| 765 | 784 |
| 766 // If there's any time left after the duration of this event
and the start | 785 // If there's any time left after the duration of this event
and the start |
| 767 // of the next, then just propagate the last value of the cu
rveData. | 786 // of the next, then just propagate the last value of the cu
rveData. |
| 768 value = curveData[numberOfCurvePoints - 1]; | 787 if (writeIndex < nextEventFillToFrame) |
| 788 value = curveData[numberOfCurvePoints - 1]; |
| 769 for (; writeIndex < nextEventFillToFrame; ++writeIndex) | 789 for (; writeIndex < nextEventFillToFrame; ++writeIndex) |
| 770 values[writeIndex] = value; | 790 values[writeIndex] = value; |
| 771 | 791 |
| 772 // Re-adjust current time | 792 // Re-adjust current time |
| 773 currentFrame = nextEventFillToFrame; | 793 currentFrame = nextEventFillToFrame; |
| 774 | 794 |
| 775 break; | 795 break; |
| 776 } | 796 } |
| 777 case ParamEvent::LastType: | 797 case ParamEvent::LastType: |
| 778 ASSERT_NOT_REACHED(); | 798 ASSERT_NOT_REACHED(); |
| 779 break; | 799 break; |
| 780 } | 800 } |
| 781 } | 801 } |
| 782 } | 802 } |
| 783 | 803 |
| 784 // If there's any time left after processing the last event then just propag
ate the last value | 804 // If there's any time left after processing the last event then just propag
ate the last value |
| 785 // to the end of the values buffer. | 805 // to the end of the values buffer. |
| 786 for (; writeIndex < numberOfValues; ++writeIndex) | 806 for (; writeIndex < numberOfValues; ++writeIndex) |
| 787 values[writeIndex] = value; | 807 values[writeIndex] = value; |
| 788 | 808 |
| 789 // This value is used to set the .value attribute of the AudioParam. | 809 // This value is used to set the .value attribute of the AudioParam. it sho
uld be the last |
| 790 return value; | 810 // computed value. |
| 811 return values[numberOfValues - 1]; |
| 791 } | 812 } |
| 792 | 813 |
| 793 } // namespace blink | 814 } // namespace blink |
| 794 | 815 |
| OLD | NEW |