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

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

Issue 2391893005: Implement clamping of AudioParam time. (Closed)
Patch Set: Address review comments Created 4 years, 2 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/Source/modules/webaudio/AudioParamTimeline.h ('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 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 double duration, 121 double duration,
122 const DOMFloat32Array* curve, 122 const DOMFloat32Array* curve,
123 float initialValue, 123 float initialValue,
124 double callTime) 124 double callTime)
125 : m_type(type), 125 : m_type(type),
126 m_value(value), 126 m_value(value),
127 m_time(time), 127 m_time(time),
128 m_timeConstant(timeConstant), 128 m_timeConstant(timeConstant),
129 m_duration(duration), 129 m_duration(duration),
130 m_initialValue(initialValue), 130 m_initialValue(initialValue),
131 m_callTime(callTime) { 131 m_callTime(callTime),
132 m_needsTimeClampCheck(true) {
132 if (curve) { 133 if (curve) {
133 // Copy the curve data 134 // Copy the curve data
134 unsigned curveLength = curve->length(); 135 unsigned curveLength = curve->length();
135 m_curve.resize(curveLength); 136 m_curve.resize(curveLength);
136 memcpy(m_curve.data(), curve->data(), curveLength * sizeof(float)); 137 memcpy(m_curve.data(), curve->data(), curveLength * sizeof(float));
137 } 138 }
138 } 139 }
139 140
140 AudioParamTimeline::ParamEvent 141 AudioParamTimeline::ParamEvent
141 AudioParamTimeline::ParamEvent::createSetValueEvent(float value, double time) { 142 AudioParamTimeline::ParamEvent::createSetValueEvent(float value, double time) {
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 return defaultValue; 450 return defaultValue;
450 451
451 // Return default value if there are no events matching the desired time 452 // Return default value if there are no events matching the desired time
452 // range. 453 // range.
453 if (!m_events.size() || (endFrame / sampleRate <= m_events[0].time())) { 454 if (!m_events.size() || (endFrame / sampleRate <= m_events[0].time())) {
454 for (unsigned i = 0; i < numberOfValues; ++i) 455 for (unsigned i = 0; i < numberOfValues; ++i)
455 values[i] = defaultValue; 456 values[i] = defaultValue;
456 return defaultValue; 457 return defaultValue;
457 } 458 }
458 459
459 // Optimize the case where the last event is in the past. 460 int numberOfEvents = m_events.size();
460 if (m_events.size() > 0) { 461
462 if (numberOfEvents > 0) {
463 double currentTime = startFrame / sampleRate;
464
465 // Look at all the events in the timeline and check to see if any needs
466 // to clamp the start time to the current time.
467 for (int k = 0; k < numberOfEvents; ++k) {
468 ParamEvent& event = m_events[k];
469
470 // We're examining the event for the first time and the event time is
471 // in the past so clamp the event time to the current time (start of
472 // the rendering quantum).
473 if (event.needsTimeClampCheck()) {
474 if (event.time() < currentTime)
475 event.setTime(currentTime);
476
477 // In all cases, we can clear the flag because the event is either
478 // in the future, or we've already checked it (just now).
479 event.clearTimeClampCheck();
480 }
481 }
482
483 // Optimize the case where the last event is in the past.
461 ParamEvent& lastEvent = m_events[m_events.size() - 1]; 484 ParamEvent& lastEvent = m_events[m_events.size() - 1];
462 ParamEvent::Type lastEventType = lastEvent.getType(); 485 ParamEvent::Type lastEventType = lastEvent.getType();
463 double lastEventTime = lastEvent.time(); 486 double lastEventTime = lastEvent.time();
464 double currentTime = startFrame / sampleRate;
465 487
466 // If the last event is in the past and the event has ended, then we can 488 // If the last event is in the past and the event has ended, then we can
467 // just propagate the same value. Except for SetTarget which lasts 489 // just propagate the same value. Except for SetTarget which lasts
468 // "forever". SetValueCurve also has an explicit SetValue at the end of 490 // "forever". SetValueCurve also has an explicit SetValue at the end of
469 // the curve, so we don't need to worry that SetValueCurve time is a 491 // the curve, so we don't need to worry that SetValueCurve time is a
470 // start time, not an end time. 492 // start time, not an end time.
471 if (lastEventTime < currentTime && lastEventType != ParamEvent::SetTarget) { 493 if (lastEventTime < currentTime && lastEventType != ParamEvent::SetTarget) {
472 // The event has finished, so just copy the default value out. 494 // The event has finished, so just copy the default value out.
473 // Since all events are now also in the past, we can just remove all 495 // Since all events are now also in the past, we can just remove all
474 // timeline events too because |defaultValue| has the expected 496 // timeline events too because |defaultValue| has the expected
(...skipping 27 matching lines...) Expand all
502 for (; writeIndex < fillToFrame; ++writeIndex) 524 for (; writeIndex < fillToFrame; ++writeIndex)
503 values[writeIndex] = defaultValue; 525 values[writeIndex] = defaultValue;
504 526
505 currentFrame += fillToFrame; 527 currentFrame += fillToFrame;
506 } 528 }
507 529
508 float value = defaultValue; 530 float value = defaultValue;
509 531
510 // Go through each event and render the value buffer where the times overlap, 532 // Go through each event and render the value buffer where the times overlap,
511 // stopping when we've rendered all the requested values. 533 // stopping when we've rendered all the requested values.
512 int n = m_events.size();
513 int lastSkippedEventIndex = 0; 534 int lastSkippedEventIndex = 0;
514 for (int i = 0; i < n && writeIndex < numberOfValues; ++i) { 535 for (int i = 0; i < numberOfEvents && writeIndex < numberOfValues; ++i) {
515 ParamEvent& event = m_events[i]; 536 ParamEvent& event = m_events[i];
516 ParamEvent* nextEvent = i < n - 1 ? &(m_events[i + 1]) : 0; 537 ParamEvent* nextEvent = i < numberOfEvents - 1 ? &(m_events[i + 1]) : 0;
517 538
518 // Wait until we get a more recent event. 539 // Wait until we get a more recent event.
519 // 540 //
520 // WARNING: due to round-off it might happen that nextEvent->time() is 541 // WARNING: due to round-off it might happen that nextEvent->time() is
521 // just larger than currentFrame/sampleRate. This means that we will end 542 // just larger than currentFrame/sampleRate. This means that we will end
522 // up running the |event| again. The code below had better be prepared 543 // up running the |event| again. The code below had better be prepared
523 // for this case! What should happen is the fillToFrame should be 0 so 544 // for this case! What should happen is the fillToFrame should be 0 so
524 // that while the event is actually run again, nothing actually gets 545 // that while the event is actually run again, nothing actually gets
525 // computed, and we move on to the next event. 546 // computed, and we move on to the next event.
526 // 547 //
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
593 exp(-(currentFrame / sampleRate - event.time()) / 614 exp(-(currentFrame / sampleRate - event.time()) /
594 event.timeConstant()); 615 event.timeConstant());
595 } else { 616 } else {
596 // SetTarget has already started. Update |value| one frame because it's 617 // SetTarget has already started. Update |value| one frame because it's
597 // the value from the previous frame. 618 // the value from the previous frame.
598 float discreteTimeConstant = static_cast<float>( 619 float discreteTimeConstant = static_cast<float>(
599 AudioUtilities::discreteTimeConstantForSampleRate( 620 AudioUtilities::discreteTimeConstantForSampleRate(
600 event.timeConstant(), controlRate)); 621 event.timeConstant(), controlRate));
601 value += (event.value() - value) * discreteTimeConstant; 622 value += (event.value() - value) * discreteTimeConstant;
602 } 623 }
624
625 // Insert a SetValueEvent to mark the starting value and time.
626 // Clear the clamp check because this doesn't need it.
603 m_events[i] = 627 m_events[i] =
604 ParamEvent::createSetValueEvent(value, currentFrame / sampleRate); 628 ParamEvent::createSetValueEvent(value, currentFrame / sampleRate);
629 m_events[i].clearTimeClampCheck();
605 } 630 }
606 631
607 float value1 = event.value(); 632 float value1 = event.value();
608 double time1 = event.time(); 633 double time1 = event.time();
609 634
610 float value2 = nextEvent ? nextEvent->value() : value1; 635 float value2 = nextEvent ? nextEvent->value() : value1;
611 double time2 = nextEvent ? nextEvent->time() : endFrame / sampleRate + 1; 636 double time2 = nextEvent ? nextEvent->time() : endFrame / sampleRate + 1;
612 637
613 double deltaTime = time2 - time1; 638 double deltaTime = time2 - time1;
614 float k = deltaTime > 0 ? 1 / deltaTime : 0; 639 float k = deltaTime > 0 ? 1 / deltaTime : 0;
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after
1047 // propagate the last value to the end of the values buffer. 1072 // propagate the last value to the end of the values buffer.
1048 for (; writeIndex < numberOfValues; ++writeIndex) 1073 for (; writeIndex < numberOfValues; ++writeIndex)
1049 values[writeIndex] = value; 1074 values[writeIndex] = value;
1050 1075
1051 // This value is used to set the .value attribute of the AudioParam. it 1076 // This value is used to set the .value attribute of the AudioParam. it
1052 // should be the last computed value. 1077 // should be the last computed value.
1053 return values[numberOfValues - 1]; 1078 return values[numberOfValues - 1];
1054 } 1079 }
1055 1080
1056 } // namespace blink 1081 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/webaudio/AudioParamTimeline.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698