Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1433)

Side by Side Diff: third_party/WebKit/Source/modules/webaudio/AudioParamTimeline.cpp

Issue 1561483004: Correctly propagate end value for exponential ramp (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/LayoutTests/webaudio/audioparam-negative-exponentialRamp-expected.txt ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
Raymond Toy 2016/01/06 22:05:02 I think this is actually incorrect and was just a
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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
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.
471 if (writeIndex < fillToFrame)
472 value = value1;
473
476 for (; writeIndex < fillToFrame; ++writeIndex) 474 for (; writeIndex < fillToFrame; ++writeIndex)
477 values[writeIndex] = value; 475 values[writeIndex] = value;
478 } else { 476 } else {
479 float numSampleFrames = deltaTime * sampleRate; 477 float numSampleFrames = deltaTime * sampleRate;
480 // The value goes exponentially from value1 to value2 in a durat ion of deltaTime 478 // The value goes exponentially from value1 to value2 in a durat ion of deltaTime
481 // seconds according to 479 // seconds according to
482 // 480 //
483 // v(t) = v1*(v2/v1)^((t-t1)/(t2-t1)) 481 // v(t) = v1*(v2/v1)^((t-t1)/(t2-t1))
484 // 482 //
485 // Let c be currentFrame and F be the sampleRate. Then we want to sample v(t) 483 // Let c be currentFrame and F be the sampleRate. Then we want to sample v(t)
(...skipping 23 matching lines...) Expand all
509 values[writeIndex] = value; 507 values[writeIndex] = value;
510 value *= multiplier; 508 value *= multiplier;
511 ++currentFrame; 509 ++currentFrame;
512 } 510 }
513 } 511 }
514 } else { 512 } else {
515 // Handle event types not requiring looking ahead to the next event. 513 // Handle event types not requiring looking ahead to the next event.
516 switch (event.type()) { 514 switch (event.type()) {
517 case ParamEvent::SetValue: 515 case ParamEvent::SetValue:
518 case ParamEvent::LinearRampToValue: 516 case ParamEvent::LinearRampToValue:
519 case ParamEvent::ExponentialRampToValue:
520 { 517 {
521 currentFrame = fillToEndFrame; 518 currentFrame = fillToEndFrame;
522 519
523 // Simply stay at a constant value. 520 // Simply stay at a constant value.
524 value = event.value(); 521 value = event.value();
525 522
526 for (; writeIndex < fillToFrame; ++writeIndex) 523 for (; writeIndex < fillToFrame; ++writeIndex)
527 values[writeIndex] = value; 524 values[writeIndex] = value;
528 525
529 break; 526 break;
530 } 527 }
531 528
529 case ParamEvent::ExponentialRampToValue:
530 {
531 currentFrame = fillToEndFrame;
532
533 // Simply stay at a constant value from the last time. We d on't want to use the
534 // value of the event in case value1 * value2 < 0. In that case we should
535 // propagate the previous value, which is in |value|.
536 for (; writeIndex < fillToFrame; ++writeIndex)
537 values[writeIndex] = value;
538
539 break;
540 }
541
532 case ParamEvent::SetTarget: 542 case ParamEvent::SetTarget:
533 { 543 {
534 // Exponential approach to target value with given time cons tant. 544 // Exponential approach to target value with given time cons tant.
535 // 545 //
536 // v(t) = v2 + (v1 - v2)*exp(-(t-t1/tau)) 546 // v(t) = v2 + (v1 - v2)*exp(-(t-t1/tau))
537 // 547 //
538 548
539 float target = event.value(); 549 float target = event.value();
540 float timeConstant = event.timeConstant(); 550 float timeConstant = event.timeConstant();
541 float discreteTimeConstant = static_cast<float>(AudioUtiliti es::discreteTimeConstantForSampleRate(timeConstant, controlRate)); 551 float discreteTimeConstant = static_cast<float>(AudioUtiliti es::discreteTimeConstantForSampleRate(timeConstant, controlRate));
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
752 // to the end of the values buffer. 762 // to the end of the values buffer.
753 for (; writeIndex < numberOfValues; ++writeIndex) 763 for (; writeIndex < numberOfValues; ++writeIndex)
754 values[writeIndex] = value; 764 values[writeIndex] = value;
755 765
756 return value; 766 return value;
757 } 767 }
758 768
759 } // namespace blink 769 } // namespace blink
760 770
761 #endif // ENABLE(WEB_AUDIO) 771 #endif // ENABLE(WEB_AUDIO)
OLDNEW
« no previous file with comments | « third_party/WebKit/LayoutTests/webaudio/audioparam-negative-exponentialRamp-expected.txt ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698