| 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 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 // unsigned and could be 0, so use currentFrame < eventFrame + 1 ins
tead. | 403 // unsigned and could be 0, so use currentFrame < eventFrame + 1 ins
tead. |
| 404 if (!((event.type() == ParamEvent::SetValue | 404 if (!((event.type() == ParamEvent::SetValue |
| 405 && (eventFrame <= currentFrame) | 405 && (eventFrame <= currentFrame) |
| 406 && (currentFrame < eventFrame + 1)))) | 406 && (currentFrame < eventFrame + 1)))) |
| 407 continue; | 407 continue; |
| 408 } | 408 } |
| 409 | 409 |
| 410 float value1 = event.value(); | 410 float value1 = event.value(); |
| 411 double time1 = event.time(); | 411 double time1 = event.time(); |
| 412 | 412 |
| 413 // If the current event is SetValue, set the default value too so that i
t appears as if | |
| 414 // SetValue were actually run for any corner caes where we want to use t
he default value. | |
| 415 if (event.type() == ParamEvent::SetValue) | |
| 416 value = value1; | |
| 417 | |
| 418 float value2 = nextEvent ? nextEvent->value() : value1; | 413 float value2 = nextEvent ? nextEvent->value() : value1; |
| 419 double time2 = nextEvent ? nextEvent->time() : endFrame / sampleRate + 1
; | 414 double time2 = nextEvent ? nextEvent->time() : endFrame / sampleRate + 1
; |
| 420 | 415 |
| 421 double deltaTime = time2 - time1; | 416 double deltaTime = time2 - time1; |
| 422 float k = deltaTime > 0 ? 1 / deltaTime : 0; | 417 float k = deltaTime > 0 ? 1 / deltaTime : 0; |
| 423 | 418 |
| 424 // |fillToEndFrame| is the exclusive upper bound of the last frame to be
computed for this | 419 // |fillToEndFrame| is the exclusive upper bound of the last frame to be
computed for this |
| 425 // event. It's either the last desired frame (|endFrame|) or derived fr
om the end time of | 420 // event. It's either the last desired frame (|endFrame|) or derived fr
om the end time of |
| 426 // the next event (time2). We compute ceil(time2*sampleRate) because fil
lToEndFrame is the | 421 // the next event (time2). We compute ceil(time2*sampleRate) because fil
lToEndFrame is the |
| 427 // exclusive upper bound. Consider the case where |startFrame| = 128 an
d time2 = 128.1 | 422 // exclusive upper bound. Consider the case where |startFrame| = 128 an
d time2 = 128.1 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 for (; writeIndex < fillToFrame; ++writeIndex) { | 460 for (; writeIndex < fillToFrame; ++writeIndex) { |
| 466 float x = (currentFrame / sampleRate - time1) * k; | 461 float x = (currentFrame / sampleRate - time1) * k; |
| 467 // value = (1 - x) * value1 + x * value2; | 462 // value = (1 - x) * value1 + x * value2; |
| 468 value = value1 + x * valueDelta; | 463 value = value1 + x * valueDelta; |
| 469 values[writeIndex] = value; | 464 values[writeIndex] = value; |
| 470 ++currentFrame; | 465 ++currentFrame; |
| 471 } | 466 } |
| 472 } else if (nextEventType == ParamEvent::ExponentialRampToValue) { | 467 } else if (nextEventType == ParamEvent::ExponentialRampToValue) { |
| 473 if (value1 * value2 <= 0) { | 468 if (value1 * value2 <= 0) { |
| 474 // It's an error if value1 and value2 have opposite signs or if
one of them is zero. | 469 // It's an error if value1 and value2 have opposite signs or if
one of them is zero. |
| 475 // Handle this by propagating the previous value. | 470 // Handle this by propagating the previous value, and making it
the default. |
| 471 value = value1; |
| 472 |
| 476 for (; writeIndex < fillToFrame; ++writeIndex) | 473 for (; writeIndex < fillToFrame; ++writeIndex) |
| 477 values[writeIndex] = value; | 474 values[writeIndex] = value; |
| 478 } else { | 475 } else { |
| 479 float numSampleFrames = deltaTime * sampleRate; | 476 float numSampleFrames = deltaTime * sampleRate; |
| 480 // The value goes exponentially from value1 to value2 in a durat
ion of deltaTime | 477 // The value goes exponentially from value1 to value2 in a durat
ion of deltaTime |
| 481 // seconds according to | 478 // seconds according to |
| 482 // | 479 // |
| 483 // v(t) = v1*(v2/v1)^((t-t1)/(t2-t1)) | 480 // v(t) = v1*(v2/v1)^((t-t1)/(t2-t1)) |
| 484 // | 481 // |
| 485 // Let c be currentFrame and F be the sampleRate. Then we want
to sample v(t) | 482 // Let c be currentFrame and F be the sampleRate. Then we want
to sample v(t) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 509 values[writeIndex] = value; | 506 values[writeIndex] = value; |
| 510 value *= multiplier; | 507 value *= multiplier; |
| 511 ++currentFrame; | 508 ++currentFrame; |
| 512 } | 509 } |
| 513 } | 510 } |
| 514 } else { | 511 } else { |
| 515 // Handle event types not requiring looking ahead to the next event. | 512 // Handle event types not requiring looking ahead to the next event. |
| 516 switch (event.type()) { | 513 switch (event.type()) { |
| 517 case ParamEvent::SetValue: | 514 case ParamEvent::SetValue: |
| 518 case ParamEvent::LinearRampToValue: | 515 case ParamEvent::LinearRampToValue: |
| 519 case ParamEvent::ExponentialRampToValue: | |
| 520 { | 516 { |
| 521 currentFrame = fillToEndFrame; | 517 currentFrame = fillToEndFrame; |
| 522 | 518 |
| 523 // Simply stay at a constant value. | 519 // Simply stay at a constant value. |
| 524 value = event.value(); | 520 value = event.value(); |
| 525 | 521 |
| 526 for (; writeIndex < fillToFrame; ++writeIndex) | 522 for (; writeIndex < fillToFrame; ++writeIndex) |
| 527 values[writeIndex] = value; | 523 values[writeIndex] = value; |
| 528 | 524 |
| 529 break; | 525 break; |
| 530 } | 526 } |
| 531 | 527 |
| 528 case ParamEvent::ExponentialRampToValue: |
| 529 { |
| 530 currentFrame = fillToEndFrame; |
| 531 |
| 532 // Simply stay at a constant value from the last time. We d
on't want to use the |
| 533 // value of the event in case value1 * value2 < 0. In this
case we should |
| 534 // propagate the previous value, which is in |value|. |
| 535 for (; writeIndex < fillToFrame; ++writeIndex) |
| 536 values[writeIndex] = value; |
| 537 |
| 538 break; |
| 539 } |
| 540 |
| 532 case ParamEvent::SetTarget: | 541 case ParamEvent::SetTarget: |
| 533 { | 542 { |
| 534 // Exponential approach to target value with given time cons
tant. | 543 // Exponential approach to target value with given time cons
tant. |
| 535 // | 544 // |
| 536 // v(t) = v2 + (v1 - v2)*exp(-(t-t1/tau)) | 545 // v(t) = v2 + (v1 - v2)*exp(-(t-t1/tau)) |
| 537 // | 546 // |
| 538 | 547 |
| 539 float target = event.value(); | 548 float target = event.value(); |
| 540 float timeConstant = event.timeConstant(); | 549 float timeConstant = event.timeConstant(); |
| 541 float discreteTimeConstant = static_cast<float>(AudioUtiliti
es::discreteTimeConstantForSampleRate(timeConstant, controlRate)); | 550 float discreteTimeConstant = static_cast<float>(AudioUtiliti
es::discreteTimeConstantForSampleRate(timeConstant, controlRate)); |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 752 // to the end of the values buffer. | 761 // to the end of the values buffer. |
| 753 for (; writeIndex < numberOfValues; ++writeIndex) | 762 for (; writeIndex < numberOfValues; ++writeIndex) |
| 754 values[writeIndex] = value; | 763 values[writeIndex] = value; |
| 755 | 764 |
| 756 return value; | 765 return value; |
| 757 } | 766 } |
| 758 | 767 |
| 759 } // namespace blink | 768 } // namespace blink |
| 760 | 769 |
| 761 #endif // ENABLE(WEB_AUDIO) | 770 #endif // ENABLE(WEB_AUDIO) |
| OLD | NEW |