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

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

Issue 1485003002: Make setTarget followed by linear or exponential ramp continuous. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add comments. Created 4 years, 7 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-setTargetAtTime-continuous-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 387 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 */;
hongchan 2016/04/27 21:40:09 Are we okay with the comment style here? I've neve
Raymond Toy 2016/04/27 21:43:15 Don't know. I just basically moved this line up f
Raymond Toy 2016/04/27 22:57:03 It means a nextEventType equal to LastType indicat
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 // 437 //
425 // However, time2 can be very large, so compute this carefully in the ca se where time2 438 // However, time2 can be very large, so compute this carefully in the ca se where time2
426 // exceeds the size of a size_t. 439 // exceeds the size of a size_t.
427 440
428 size_t fillToEndFrame = endFrame; 441 size_t fillToEndFrame = endFrame;
429 if (endFrame > time2 * sampleRate) 442 if (endFrame > time2 * sampleRate)
430 fillToEndFrame = static_cast<size_t>(ceil(time2 * sampleRate)); 443 fillToEndFrame = static_cast<size_t>(ceil(time2 * sampleRate));
431 444
432 ASSERT(fillToEndFrame >= startFrame); 445 ASSERT(fillToEndFrame >= startFrame);
433 size_t fillToFrame = fillToEndFrame - startFrame; 446 size_t fillToFrame = fillToEndFrame - startFrame;
434 fillToFrame = std::min(fillToFrame, static_cast<size_t>(numberOfValues)) ; 447 fillToFrame = std::min(fillToFrame, static_cast<size_t>(numberOfValues)) ;
435 448
436 ParamEvent::Type nextEventType = nextEvent ? static_cast<ParamEvent::Typ e>(nextEvent->type()) : ParamEvent::LastType /* unknown */;
437 449
438 // First handle linear and exponential ramps which require looking ahead to the next event. 450 // First handle linear and exponential ramps which require looking ahead to the next event.
439 if (nextEventType == ParamEvent::LinearRampToValue) { 451 if (nextEventType == ParamEvent::LinearRampToValue) {
440 const float valueDelta = value2 - value1; 452 const float valueDelta = value2 - value1;
441 #if CPU(X86) || CPU(X86_64) 453 #if CPU(X86) || CPU(X86_64)
442 // Minimize in-loop operations. Calculate starting value and increme nt. Next step: value += inc. 454 // Minimize in-loop operations. Calculate starting value and increme nt. Next step: value += inc.
443 // value = value1 + (currentFrame/sampleRate - time1) * k * (value2 - value1); 455 // value = value1 + (currentFrame/sampleRate - time1) * k * (value2 - value1);
444 // inc = 4 / sampleRate * k * (value2 - value1); 456 // inc = 4 / sampleRate * k * (value2 - value1);
445 // Resolve recursion by expanding constants to achieve a 4-step loop unrolling. 457 // Resolve recursion by expanding constants to achieve a 4-step loop unrolling.
446 // value = value1 + ((currentFrame/sampleRate - time1) + i * sample FrameTimeIncr) * k * (value2 -value1), i in 0..3 458 // 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
510 float multiplier = powf(value2 / value1, 1 / numSampleFrames); 522 float multiplier = powf(value2 / value1, 1 / numSampleFrames);
511 // Set the starting value of the exponential ramp. 523 // Set the starting value of the exponential ramp.
512 value = value1 * powf(value2 / value1, 524 value = value1 * powf(value2 / value1,
513 (currentFrame / sampleRate - time1) / deltaTime); 525 (currentFrame / sampleRate - time1) / deltaTime);
514 526
515 for (; writeIndex < fillToFrame; ++writeIndex) { 527 for (; writeIndex < fillToFrame; ++writeIndex) {
516 values[writeIndex] = value; 528 values[writeIndex] = value;
517 value *= multiplier; 529 value *= multiplier;
518 ++currentFrame; 530 ++currentFrame;
519 } 531 }
532 // Due to roundoff it's possible that value exceeds value2. Cli p value to value2 if
533 // we are within 1/2 frame of time2.
534 if (currentFrame > time2 * sampleRate - 0.5)
535 value = value2;
520 } 536 }
521 } else { 537 } else {
522 // Handle event types not requiring looking ahead to the next event. 538 // Handle event types not requiring looking ahead to the next event.
523 switch (event.type()) { 539 switch (event.type()) {
524 case ParamEvent::SetValue: 540 case ParamEvent::SetValue:
525 case ParamEvent::LinearRampToValue: 541 case ParamEvent::LinearRampToValue:
526 { 542 {
527 currentFrame = fillToEndFrame; 543 currentFrame = fillToEndFrame;
528 544
529 // Simply stay at a constant value. 545 // Simply stay at a constant value.
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
771 // to the end of the values buffer. 787 // to the end of the values buffer.
772 for (; writeIndex < numberOfValues; ++writeIndex) 788 for (; writeIndex < numberOfValues; ++writeIndex)
773 values[writeIndex] = value; 789 values[writeIndex] = value;
774 790
775 // This value is used to set the .value attribute of the AudioParam. 791 // This value is used to set the .value attribute of the AudioParam.
776 return value; 792 return value;
777 } 793 }
778 794
779 } // namespace blink 795 } // namespace blink
780 796
OLDNEW
« no previous file with comments | « third_party/WebKit/LayoutTests/webaudio/audioparam-setTargetAtTime-continuous-expected.txt ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698