File: panner_handler.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (798 lines) | stat: -rw-r--r-- 29,658 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/webaudio/panner_handler.h"

#include <array>

#include "base/compiler_specific.h"
#include "base/metrics/histogram_functions.h"
#include "base/synchronization/lock.h"
#include "third_party/blink/renderer/modules/webaudio/audio_listener.h"
#include "third_party/blink/renderer/modules/webaudio/audio_node_input.h"
#include "third_party/blink/renderer/modules/webaudio/audio_node_output.h"
#include "third_party/blink/renderer/modules/webaudio/audio_param.h"
#include "third_party/blink/renderer/modules/webaudio/base_audio_context.h"
#include "third_party/blink/renderer/platform/audio/audio_bus.h"
#include "third_party/blink/renderer/platform/bindings/exception_messages.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"
#include "third_party/blink/renderer/platform/wtf/math_extras.h"

namespace blink {

namespace {

// A PannerNode only supports 1 or 2 channels.
constexpr unsigned kMinimumOutputChannels = 1;
constexpr unsigned kMaximumOutputChannels = 2;

void FixNANs(double& x) {
  if (!std::isfinite(x)) {
    x = 0.0;
  }
}

}  // namespace

PannerHandler::PannerHandler(AudioNode& node,
                             float sample_rate,
                             AudioParamHandler& position_x,
                             AudioParamHandler& position_y,
                             AudioParamHandler& position_z,
                             AudioParamHandler& orientation_x,
                             AudioParamHandler& orientation_y,
                             AudioParamHandler& orientation_z)
    : AudioHandler(NodeType::kNodeTypePanner, node, sample_rate),
      position_x_(&position_x),
      position_y_(&position_y),
      position_z_(&position_z),
      orientation_x_(&orientation_x),
      orientation_y_(&orientation_y),
      orientation_z_(&orientation_z),
      listener_handler_(&node.context()->listener()->Handler()) {
  AddInput();
  AddOutput(kMaximumOutputChannels);

  // Node-specific default mixing rules
  channel_count_ = kMaximumOutputChannels;
  SetInternalChannelCountMode(V8ChannelCountMode::Enum::kClampedMax);
  SetInternalChannelInterpretation(AudioBus::kSpeakers);

  // Explicitly set the default panning model here so that the histograms
  // include the default value.
  SetPanningModel(V8PanningModelType::Enum::kEqualpower);

  Initialize();
}

scoped_refptr<PannerHandler> PannerHandler::Create(
    AudioNode& node,
    float sample_rate,
    AudioParamHandler& position_x,
    AudioParamHandler& position_y,
    AudioParamHandler& position_z,
    AudioParamHandler& orientation_x,
    AudioParamHandler& orientation_y,
    AudioParamHandler& orientation_z) {
  return base::AdoptRef(new PannerHandler(node, sample_rate, position_x,
                                          position_y, position_z, orientation_x,
                                          orientation_y, orientation_z));
}

PannerHandler::~PannerHandler() {
  Uninitialize();
}

// PannerNode needs a custom ProcessIfNecessary to get the process lock when
// computing PropagatesSilence() to protect processing from changes happening to
// the panning model.  This is very similar to AudioNode::ProcessIfNecessary.
void PannerHandler::ProcessIfNecessary(uint32_t frames_to_process) {
  DCHECK(Context()->IsAudioThread());

  if (!IsInitialized()) {
    return;
  }

  // Ensure that we only process once per rendering quantum.
  // This handles the "fanout" problem where an output is connected to multiple
  // inputs.  The first time we're called during this time slice we process, but
  // after that we don't want to re-process, instead our output(s) will already
  // have the results cached in their bus
  const double current_time = Context()->currentTime();
  if (last_processing_time_ != current_time) {
    // important to first update this time because of feedback loops in the
    // rendering graph.
    last_processing_time_ = current_time;

    PullInputs(frames_to_process);

    const bool silent_inputs = InputsAreSilent();

    {
      // Need to protect calls to PropagatesSilence (and Process) because the
      // main thread may be changing the panning model that modifies the
      // TailTime and LatencyTime methods called by PropagatesSilence.
      base::AutoTryLock try_locker(process_lock_);
      if (try_locker.is_acquired()) {
        if (silent_inputs && PropagatesSilence()) {
          SilenceOutputs();
          // AudioParams still need to be processed so that the value can be
          // updated if there are automations or so that the upstream nodes get
          // pulled if any are connected to the AudioParam.
          ProcessOnlyAudioParams(frames_to_process);
        } else {
          // Unsilence the outputs first because the processing of the node may
          // cause the outputs to go silent and we want to propagate that hint
          // to the downstream nodes.  (For example, a Gain node with a gain of
          // 0 will want to silence its output.)
          UnsilenceOutputs();
          Process(frames_to_process);
        }
      } else {
        // We must be in the middle of changing the properties of the panner.
        // Just output silence.
        AudioBus* destination = Output(0).Bus();
        destination->Zero();
      }
    }

    if (!silent_inputs) {
      // Update `last_non_silent_time` AFTER processing this block.
      // Doing it before causes `PropagateSilence()` to be one render
      // quantum longer than necessary.
      last_non_silent_time_ =
          (Context()->CurrentSampleFrame() + frames_to_process) /
          static_cast<double>(Context()->sampleRate());
    }
  }
}

void PannerHandler::Process(uint32_t frames_to_process) {
  TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("webaudio.audionode"),
               "PannerHandler::Process");

  AudioBus* destination = Output(0).Bus();

  if (!IsInitialized() || !panner_.get()) {
    destination->Zero();
    return;
  }

  scoped_refptr<AudioBus> source = Input(0).Bus();
  if (!source) {
    destination->Zero();
    return;
  }

  // The audio thread can't block on this lock, so we call tryLock() instead.
  base::AutoTryLock try_listener_locker(listener_handler_->Lock());

  if (try_listener_locker.is_acquired()) {
    if (!Context()->HasRealtimeConstraint() &&
        panning_model_ == Panner::PanningModel::kHRTF) {
      // For an OfflineAudioContext, we need to make sure the HRTFDatabase
      // is loaded before proceeding.  For realtime contexts, we don't
      // have to wait.  The HRTF panner handles that case itself.
      listener_handler_->WaitForHRTFDatabaseLoaderThreadCompletion();
    }

    if ((HasSampleAccurateValues() ||
         listener_handler_->HasSampleAccurateValues()) &&
        (IsAudioRate() || listener_handler_->IsAudioRate())) {
      // It's tempting to skip sample-accurate processing if
      // isAzimuthElevationDirty() and isDistanceConeGain() both return false.
      // But in general we can't because something may scheduled to start in the
      // middle of the rendering quantum.  On the other hand, the audible effect
      // may be small enough that we can afford to do this optimization.
      ProcessSampleAccurateValues(destination, source.get(), frames_to_process);
    } else {
      // Apply the panning effect.
      double azimuth;
      double elevation;

      // Update dirty state in case something has moved; this can happen if the
      // AudioParam for the position or orientation component is set directly.
      UpdateDirtyState();

      AzimuthElevation(&azimuth, &elevation);

      panner_->Pan(azimuth, elevation, source.get(), destination,
                   frames_to_process, InternalChannelInterpretation());

      // Get the distance and cone gain.
      const float total_gain = DistanceConeGain();

      // Apply gain in-place.
      destination->CopyWithGainFrom(*destination, total_gain);
    }
  } else {
    // Too bad - The tryLock() failed.  We must be in the middle of changing the
    // properties of the panner or the listener.
    destination->Zero();
  }
}

void PannerHandler::ProcessSampleAccurateValues(AudioBus* destination,
                                                const AudioBus* source,
                                                uint32_t frames_to_process) {
  // TODO(crbug.com/40637820): Eventually, the render quantum size will no
  // longer be hardcoded as 128. At that point, we'll need to switch from
  // stack allocation to heap allocation.
  constexpr unsigned render_quantum_frames_expected = 128;
  const unsigned render_quantum_frames =
      GetDeferredTaskHandler().RenderQuantumFrames();
  CHECK_EQ(render_quantum_frames, render_quantum_frames_expected);
  CHECK_LE(frames_to_process, render_quantum_frames_expected);

  float panner_x[render_quantum_frames_expected];
  float panner_y[render_quantum_frames_expected];
  float panner_z[render_quantum_frames_expected];
  float orientation_x[render_quantum_frames_expected];
  float orientation_y[render_quantum_frames_expected];
  float orientation_z[render_quantum_frames_expected];
  std::array<double, render_quantum_frames_expected> azimuth;
  std::array<double, render_quantum_frames_expected> elevation;
  float total_gain[render_quantum_frames_expected];

  position_x_->CalculateSampleAccurateValues(
      base::span(panner_x).first(frames_to_process));
  position_y_->CalculateSampleAccurateValues(
      base::span(panner_y).first(frames_to_process));
  position_z_->CalculateSampleAccurateValues(
      base::span(panner_z).first(frames_to_process));
  orientation_x_->CalculateSampleAccurateValues(
      base::span(orientation_x).first(frames_to_process));
  orientation_y_->CalculateSampleAccurateValues(
      base::span(orientation_y).first(frames_to_process));
  orientation_z_->CalculateSampleAccurateValues(
      base::span(orientation_z).first(frames_to_process));

  const float* listener_x = listener_handler_->GetPositionXValues(
      render_quantum_frames);
  const float* listener_y = listener_handler_->GetPositionYValues(
      render_quantum_frames);
  const float* listener_z = listener_handler_->GetPositionZValues(
      render_quantum_frames);
  const float* forward_x = listener_handler_->GetForwardXValues(
      render_quantum_frames);
  const float* forward_y = listener_handler_->GetForwardYValues(
      render_quantum_frames);
  const float* forward_z = listener_handler_->GetForwardZValues(
      render_quantum_frames);
  const float* up_x = listener_handler_->GetUpXValues(
      render_quantum_frames);
  const float* up_y = listener_handler_->GetUpYValues(
      render_quantum_frames);
  const float* up_z = listener_handler_->GetUpZValues(
      render_quantum_frames);

  UNSAFE_TODO({
    // Compute the azimuth, elevation, and total gains for each position.
    for (unsigned k = 0; k < frames_to_process; ++k) {
      gfx::Point3F panner_position(panner_x[k], panner_y[k], panner_z[k]);
      gfx::Vector3dF orientation(orientation_x[k], orientation_y[k],
                                 orientation_z[k]);
      gfx::Point3F listener_position(listener_x[k], listener_y[k],
                                     listener_z[k]);
      gfx::Vector3dF listener_forward(forward_x[k], forward_y[k], forward_z[k]);
      gfx::Vector3dF listener_up(up_x[k], up_y[k], up_z[k]);

      CalculateAzimuthElevation(&azimuth[k], &elevation[k], panner_position,
                                listener_position, listener_forward,
                                listener_up);

      total_gain[k] = CalculateDistanceConeGain(panner_position, orientation,
                                                listener_position);
    }
    // Update cached values in case automations end.
    if (frames_to_process > 0) {
      cached_azimuth_ = azimuth[frames_to_process - 1];
      cached_elevation_ = elevation[frames_to_process - 1];
      cached_distance_cone_gain_ = total_gain[frames_to_process - 1];
    }
  });
  panner_->PanWithSampleAccurateValues(azimuth, elevation, source, destination,
                                       frames_to_process,
                                       InternalChannelInterpretation());
  destination->CopyWithSampleAccurateGainValuesFrom(*destination, total_gain,
                                                    frames_to_process);
}

void PannerHandler::ProcessOnlyAudioParams(uint32_t frames_to_process) {
  // TODO(crbug.com/40637820): Eventually, the render quantum size will no
  // longer be hardcoded as 128. At that point, we'll need to switch from
  // stack allocation to heap allocation.
  constexpr unsigned render_quantum_frames_expected = 128;
  CHECK_EQ(GetDeferredTaskHandler().RenderQuantumFrames(),
           render_quantum_frames_expected);
  float values[render_quantum_frames_expected];

  DCHECK_LE(frames_to_process, GetDeferredTaskHandler().RenderQuantumFrames());

  position_x_->CalculateSampleAccurateValues(
      base::span(values).first(frames_to_process));
  position_y_->CalculateSampleAccurateValues(
      base::span(values).first(frames_to_process));
  position_z_->CalculateSampleAccurateValues(
      base::span(values).first(frames_to_process));
  orientation_x_->CalculateSampleAccurateValues(
      base::span(values).first(frames_to_process));
  orientation_y_->CalculateSampleAccurateValues(
      base::span(values).first(frames_to_process));
  orientation_z_->CalculateSampleAccurateValues(
      base::span(values).first(frames_to_process));
}

void PannerHandler::Initialize() {
  if (IsInitialized()) {
    return;
  }

  panner_ = Panner::Create(panning_model_, Context()->sampleRate(),
                           GetDeferredTaskHandler().RenderQuantumFrames(),
                           listener_handler_->HrtfDatabaseLoader());
  listener_handler_->AddPannerHandler(*this);

  // The panner is already marked as dirty, so `last_position_` and
  // `last_orientation_` will be updated on first use.  Don't need to
  // set them here.

  AudioHandler::Initialize();
}

void PannerHandler::Uninitialize() {
  if (!IsInitialized()) {
    return;
  }

  // Unlike AudioHandlers, there is no orphan handler treatment for the
  // AudioListenerHandler, so it can be nullptr if the context is already GCed.
  if (listener_handler_) {
    listener_handler_->RemovePannerHandler(*this);
    listener_handler_.reset();
  }
  panner_.reset();

  AudioHandler::Uninitialize();
}

V8PanningModelType::Enum PannerHandler::PanningModel() const {
  switch (panning_model_) {
    case Panner::PanningModel::kEqualPower:
      return V8PanningModelType::Enum::kEqualpower;
    case Panner::PanningModel::kHRTF:
      return V8PanningModelType::Enum::kHRTF;
  }
  NOTREACHED();
}

void PannerHandler::SetPanningModel(V8PanningModelType::Enum model) {
  // WebIDL should guarantee that we are never called with an invalid string
  // for the model.
  switch (model) {
    case V8PanningModelType::Enum::kEqualpower:
      SetPanningModel(Panner::PanningModel::kEqualPower);
      return;
    case V8PanningModelType::Enum::kHRTF:
      SetPanningModel(Panner::PanningModel::kHRTF);
      return;
  }
  NOTREACHED();
}

// This method should only be called from setPanningModel(const String&)!
bool PannerHandler::SetPanningModel(Panner::PanningModel model) {
  base::UmaHistogramEnumeration("WebAudio.PannerNode.PanningModel", model);

  if (model == Panner::PanningModel::kHRTF) {
    // Load the HRTF database asynchronously so we don't block the
    // Javascript thread while creating the HRTF database. It's ok to call
    // this multiple times; we won't be constantly loading the database over
    // and over.
    listener_handler_->CreateAndLoadHRTFDatabaseLoader(Context()->sampleRate());
  }

  if (!panner_.get() || model != panning_model_) {
    // We need the graph lock to secure the panner backend because
    // BaseAudioContext::Handle{Pre,Post}RenderTasks() from the audio thread
    // can touch it.
    DeferredTaskHandler::GraphAutoLocker context_locker(Context());

    // This synchronizes with process().
    base::AutoLock process_locker(process_lock_);
    panner_ = Panner::Create(model, Context()->sampleRate(),
                             GetDeferredTaskHandler().RenderQuantumFrames(),
                             listener_handler_->HrtfDatabaseLoader());
    panning_model_ = model;
  }
  return true;
}

V8DistanceModelType::Enum PannerHandler::DistanceModel() const {
  switch (const_cast<PannerHandler*>(this)->distance_effect_.Model()) {
    case DistanceEffect::kModelLinear:
      return V8DistanceModelType::Enum::kLinear;
    case DistanceEffect::kModelInverse:
      return V8DistanceModelType::Enum::kInverse;
    case DistanceEffect::kModelExponential:
      return V8DistanceModelType::Enum::kExponential;
  }
  NOTREACHED();
}

void PannerHandler::SetDistanceModel(V8DistanceModelType::Enum model) {
  switch (model) {
    case V8DistanceModelType::Enum::kLinear:
      SetDistanceModel(DistanceEffect::kModelLinear);
      return;
    case V8DistanceModelType::Enum::kInverse:
      SetDistanceModel(DistanceEffect::kModelInverse);
      return;
    case V8DistanceModelType::Enum::kExponential:
      SetDistanceModel(DistanceEffect::kModelExponential);
      return;
  }
  NOTREACHED();
}

bool PannerHandler::SetDistanceModel(unsigned model) {
  switch (model) {
    case DistanceEffect::kModelLinear:
    case DistanceEffect::kModelInverse:
    case DistanceEffect::kModelExponential:
      if (model != distance_model_) {
        // This synchronizes with process().
        base::AutoLock process_locker(process_lock_);
        distance_effect_.SetModel(
            static_cast<DistanceEffect::ModelType>(model));
        distance_model_ = model;
      }
      break;
    default:
      NOTREACHED();
  }

  return true;
}

void PannerHandler::SetRefDistance(double distance) {
  if (RefDistance() == distance) {
    return;
  }

  // This synchronizes with process().
  base::AutoLock process_locker(process_lock_);
  distance_effect_.SetRefDistance(distance);
  MarkPannerAsDirty(PannerHandler::kDistanceConeGainDirty);
}

void PannerHandler::SetMaxDistance(double distance) {
  if (MaxDistance() == distance) {
    return;
  }

  // This synchronizes with process().
  base::AutoLock process_locker(process_lock_);
  distance_effect_.SetMaxDistance(distance);
  MarkPannerAsDirty(PannerHandler::kDistanceConeGainDirty);
}

void PannerHandler::SetRolloffFactor(double factor) {
  if (RolloffFactor() == factor) {
    return;
  }

  // This synchronizes with process().
  base::AutoLock process_locker(process_lock_);
  distance_effect_.SetRolloffFactor(factor);
  MarkPannerAsDirty(PannerHandler::kDistanceConeGainDirty);
}

void PannerHandler::SetConeInnerAngle(double angle) {
  if (ConeInnerAngle() == angle) {
    return;
  }

  // This synchronizes with process().
  base::AutoLock process_locker(process_lock_);
  cone_effect_.SetInnerAngle(angle);
  MarkPannerAsDirty(PannerHandler::kDistanceConeGainDirty);
}

void PannerHandler::SetConeOuterAngle(double angle) {
  if (ConeOuterAngle() == angle) {
    return;
  }

  // This synchronizes with process().
  base::AutoLock process_locker(process_lock_);
  cone_effect_.SetOuterAngle(angle);
  MarkPannerAsDirty(PannerHandler::kDistanceConeGainDirty);
}

void PannerHandler::SetConeOuterGain(double angle) {
  if (ConeOuterGain() == angle) {
    return;
  }

  // This synchronizes with process().
  base::AutoLock process_locker(process_lock_);
  cone_effect_.SetOuterGain(angle);
  MarkPannerAsDirty(PannerHandler::kDistanceConeGainDirty);
}

void PannerHandler::SetPosition(float x,
                                float y,
                                float z,
                                ExceptionState& exceptionState) {
  // This synchronizes with process().
  base::AutoLock process_locker(process_lock_);

  double now = Context()->currentTime();

  position_x_->Timeline().SetValueAtTime(x, now, exceptionState);
  position_y_->Timeline().SetValueAtTime(y, now, exceptionState);
  position_z_->Timeline().SetValueAtTime(z, now, exceptionState);

  MarkPannerAsDirty(PannerHandler::kAzimuthElevationDirty |
                    PannerHandler::kDistanceConeGainDirty);
}

void PannerHandler::SetOrientation(float x,
                                   float y,
                                   float z,
                                   ExceptionState& exceptionState) {
  // This synchronizes with process().
  base::AutoLock process_locker(process_lock_);

  double now = Context()->currentTime();

  orientation_x_->Timeline().SetValueAtTime(x, now, exceptionState);
  orientation_y_->Timeline().SetValueAtTime(y, now, exceptionState);
  orientation_z_->Timeline().SetValueAtTime(z, now, exceptionState);

  MarkPannerAsDirty(PannerHandler::kDistanceConeGainDirty);
}

void PannerHandler::CalculateAzimuthElevation(
    double* out_azimuth,
    double* out_elevation,
    const gfx::Point3F& position,
    const gfx::Point3F& listener_position,
    const gfx::Vector3dF& listener_forward,
    const gfx::Vector3dF& listener_up) {
  // Calculate the source-listener vector
  gfx::Vector3dF source_listener = position - listener_position;

  // Quick default return if the source and listener are at the same position.
  if (!source_listener.GetNormalized(&source_listener)) {
    *out_azimuth = 0;
    *out_elevation = 0;
    return;
  }

  // Align axes
  gfx::Vector3dF listener_right =
      gfx::CrossProduct(listener_forward, listener_up);
  listener_right.GetNormalized(&listener_right);

  gfx::Vector3dF listener_forward_norm = listener_forward;
  listener_forward_norm.GetNormalized(&listener_forward_norm);

  gfx::Vector3dF up = gfx::CrossProduct(listener_right, listener_forward_norm);

  float up_projection = gfx::DotProduct(source_listener, up);

  gfx::Vector3dF projected_source =
      source_listener - gfx::ScaleVector3d(up, up_projection);
  projected_source.GetNormalized(&projected_source);

  // Don't use gfx::AngleBetweenVectorsInDegrees here.  It produces the wrong
  // value when one of the vectors has zero length.  We know here that
  // `projected_source` and `listener_right` are "normalized", so the dot
  // product is good enough.
  double azimuth = Rad2deg(acos(
      ClampTo(gfx::DotProduct(projected_source, listener_right), -1.0f, 1.0f)));
  FixNANs(azimuth);  // avoid illegal values

  // Source  in front or behind the listener
  double front_back = gfx::DotProduct(projected_source, listener_forward_norm);
  if (front_back < 0.0) {
    azimuth = 360.0 - azimuth;
  }

  // Make azimuth relative to "front" and not "right" listener vector
  if ((azimuth >= 0.0) && (azimuth <= 270.0)) {
    azimuth = 90.0 - azimuth;
  } else {
    azimuth = 450.0 - azimuth;
  }

  // Elevation
  double elevation =
      90 - gfx::AngleBetweenVectorsInDegrees(source_listener, up);
  FixNANs(elevation);  // avoid illegal values

  if (elevation > 90.0) {
    elevation = 180.0 - elevation;
  } else if (elevation < -90.0) {
    elevation = -180.0 - elevation;
  }

  if (out_azimuth) {
    *out_azimuth = azimuth;
  }
  if (out_elevation) {
    *out_elevation = elevation;
  }
}

float PannerHandler::CalculateDistanceConeGain(
    const gfx::Point3F& position,
    const gfx::Vector3dF& orientation,
    const gfx::Point3F& listener_position) {
  double listener_distance = (position - listener_position).Length();
  double distance_gain = distance_effect_.Gain(listener_distance);
  double cone_gain =
      cone_effect_.Gain(position, orientation, listener_position);

  return static_cast<float>(distance_gain * cone_gain);
}

void PannerHandler::AzimuthElevation(double* out_azimuth,
                                     double* out_elevation) {
  DCHECK(Context()->IsAudioThread());

  // Calculate new azimuth and elevation if the panner or the listener changed
  // position or orientation in any way.
  if (IsAzimuthElevationDirty() || listener_handler_->IsListenerDirty()) {
    CalculateAzimuthElevation(
        &cached_azimuth_, &cached_elevation_, GetPosition(),
        listener_handler_->GetPosition(),
        listener_handler_->GetOrientation(),
        listener_handler_->GetUpVector());
    is_azimuth_elevation_dirty_ = false;
  }

  *out_azimuth = cached_azimuth_;
  *out_elevation = cached_elevation_;
}

float PannerHandler::DistanceConeGain() {
  DCHECK(Context()->IsAudioThread());

  // Calculate new distance and cone gain if the panner or the listener
  // changed position or orientation in any way.
  if (IsDistanceConeGainDirty() || listener_handler_->IsListenerDirty()) {
    cached_distance_cone_gain_ = CalculateDistanceConeGain(
        GetPosition(), Orientation(), listener_handler_->GetPosition());
    is_distance_cone_gain_dirty_ = false;
  }

  return cached_distance_cone_gain_;
}

void PannerHandler::MarkPannerAsDirty(unsigned dirty) {
  if (dirty & PannerHandler::kAzimuthElevationDirty) {
    is_azimuth_elevation_dirty_ = true;
  }

  if (dirty & PannerHandler::kDistanceConeGainDirty) {
    is_distance_cone_gain_dirty_ = true;
  }
}

void PannerHandler::SetChannelCount(unsigned channel_count,
                                    ExceptionState& exception_state) {
  DCHECK(IsMainThread());
  DeferredTaskHandler::GraphAutoLocker locker(Context());

  if (channel_count >= kMinimumOutputChannels &&
      channel_count <= kMaximumOutputChannels) {
    if (channel_count_ != channel_count) {
      channel_count_ = channel_count;
      if (InternalChannelCountMode() != V8ChannelCountMode::Enum::kMax) {
        UpdateChannelsForInputs();
      }
    }
  } else {
    exception_state.ThrowDOMException(
        DOMExceptionCode::kNotSupportedError,
        ExceptionMessages::IndexOutsideRange<uint32_t>(
            "channelCount", channel_count, kMinimumOutputChannels,
            ExceptionMessages::kInclusiveBound, kMaximumOutputChannels,
            ExceptionMessages::kInclusiveBound));
  }
}

void PannerHandler::SetChannelCountMode(V8ChannelCountMode::Enum mode,
                                        ExceptionState& exception_state) {
  DCHECK(IsMainThread());
  DeferredTaskHandler::GraphAutoLocker locker(Context());

  V8ChannelCountMode::Enum old_mode = InternalChannelCountMode();

  if (mode == V8ChannelCountMode::Enum::kClampedMax ||
      mode == V8ChannelCountMode::Enum::kExplicit) {
    new_channel_count_mode_ = mode;
  } else if (mode == V8ChannelCountMode::Enum::kMax) {
    // This is not supported for a PannerNode, which can only handle 1 or 2
    // channels.
    exception_state.ThrowDOMException(DOMExceptionCode::kNotSupportedError,
                                      "Panner: 'max' is not allowed");
    new_channel_count_mode_ = old_mode;
  } else {
    // Do nothing for other invalid values.
    new_channel_count_mode_ = old_mode;
  }

  if (new_channel_count_mode_ != old_mode) {
    Context()->GetDeferredTaskHandler().AddChangedChannelCountMode(this);
  }
}

gfx::Point3F PannerHandler::GetPosition() const {
  auto x = position_x_->IsAudioRate() ? position_x_->FinalValue()
                                      : position_x_->Value();
  auto y = position_y_->IsAudioRate() ? position_y_->FinalValue()
                                      : position_y_->Value();
  auto z = position_z_->IsAudioRate() ? position_z_->FinalValue()
                                      : position_z_->Value();

  return gfx::Point3F(x, y, z);
}

gfx::Vector3dF PannerHandler::Orientation() const {
  auto x = orientation_x_->IsAudioRate() ? orientation_x_->FinalValue()
                                         : orientation_x_->Value();
  auto y = orientation_y_->IsAudioRate() ? orientation_y_->FinalValue()
                                         : orientation_y_->Value();
  auto z = orientation_z_->IsAudioRate() ? orientation_z_->FinalValue()
                                         : orientation_z_->Value();

  return gfx::Vector3dF(x, y, z);
}

bool PannerHandler::HasSampleAccurateValues() const {
  return position_x_->HasSampleAccurateValues() ||
         position_y_->HasSampleAccurateValues() ||
         position_z_->HasSampleAccurateValues() ||
         orientation_x_->HasSampleAccurateValues() ||
         orientation_y_->HasSampleAccurateValues() ||
         orientation_z_->HasSampleAccurateValues();
}

bool PannerHandler::IsAudioRate() const {
  return position_x_->IsAudioRate() || position_y_->IsAudioRate() ||
         position_z_->IsAudioRate() || orientation_x_->IsAudioRate() ||
         orientation_y_->IsAudioRate() || orientation_z_->IsAudioRate();
}

void PannerHandler::UpdateDirtyState() {
  DCHECK(Context()->IsAudioThread());

  gfx::Point3F current_position = GetPosition();
  gfx::Vector3dF current_orientation = Orientation();

  bool has_moved = current_position != last_position_ ||
                   current_orientation != last_orientation_;

  if (has_moved) {
    last_position_ = current_position;
    last_orientation_ = current_orientation;

    MarkPannerAsDirty(PannerHandler::kAzimuthElevationDirty |
                      PannerHandler::kDistanceConeGainDirty);
  }
}

bool PannerHandler::RequiresTailProcessing() const {
  // If there's no internal panner method set up yet, assume we require tail
  // processing in case the HRTF panner is set later, which does require tail
  // processing.
  return panner_ ? panner_->RequiresTailProcessing() : true;
}

}  // namespace blink