OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010, Google Inc. All rights reserved. | 2 * Copyright (C) 2010, 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 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 x = 0.0; | 42 x = 0.0; |
43 } | 43 } |
44 | 44 |
45 PannerHandler::PannerHandler(AudioNode& node, float sampleRate) | 45 PannerHandler::PannerHandler(AudioNode& node, float sampleRate) |
46 : AudioHandler(NodeTypePanner, node, sampleRate) | 46 : AudioHandler(NodeTypePanner, node, sampleRate) |
47 , m_listener(node.context()->listener()) | 47 , m_listener(node.context()->listener()) |
48 , m_panningModel(Panner::PanningModelEqualPower) | 48 , m_panningModel(Panner::PanningModelEqualPower) |
49 , m_distanceModel(DistanceEffect::ModelInverse) | 49 , m_distanceModel(DistanceEffect::ModelInverse) |
50 , m_position(0, 0, 0) | 50 , m_position(0, 0, 0) |
51 , m_orientation(1, 0, 0) | 51 , m_orientation(1, 0, 0) |
52 , m_velocity(0, 0, 0) | |
53 , m_isAzimuthElevationDirty(true) | 52 , m_isAzimuthElevationDirty(true) |
54 , m_isDistanceConeGainDirty(true) | 53 , m_isDistanceConeGainDirty(true) |
55 , m_isDopplerRateDirty(true) | |
56 , m_lastGain(-1.0) | 54 , m_lastGain(-1.0) |
57 , m_cachedAzimuth(0) | 55 , m_cachedAzimuth(0) |
58 , m_cachedElevation(0) | 56 , m_cachedElevation(0) |
59 , m_cachedDistanceConeGain(1.0f) | 57 , m_cachedDistanceConeGain(1.0f) |
60 , m_cachedDopplerRate(1) | |
61 { | 58 { |
62 // Load the HRTF database asynchronously so we don't block the Javascript th
read while creating the HRTF database. | 59 // Load the HRTF database asynchronously so we don't block the Javascript th
read while creating the HRTF database. |
63 // The HRTF panner will return zeroes until the database is loaded. | 60 // The HRTF panner will return zeroes until the database is loaded. |
64 listener()->createAndLoadHRTFDatabaseLoader(node.context()->sampleRate()); | 61 listener()->createAndLoadHRTFDatabaseLoader(node.context()->sampleRate()); |
65 | 62 |
66 addInput(); | 63 addInput(); |
67 addOutput(2); | 64 addOutput(2); |
68 | 65 |
69 // Node-specific default mixing rules. | 66 // Node-specific default mixing rules. |
70 m_channelCount = 2; | 67 m_channelCount = 2; |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 void PannerHandler::setPosition(float x, float y, float z) | 318 void PannerHandler::setPosition(float x, float y, float z) |
322 { | 319 { |
323 FloatPoint3D position = FloatPoint3D(x, y, z); | 320 FloatPoint3D position = FloatPoint3D(x, y, z); |
324 | 321 |
325 if (m_position == position) | 322 if (m_position == position) |
326 return; | 323 return; |
327 | 324 |
328 // This synchronizes with process(). | 325 // This synchronizes with process(). |
329 MutexLocker processLocker(m_processLock); | 326 MutexLocker processLocker(m_processLock); |
330 m_position = position; | 327 m_position = position; |
331 markPannerAsDirty(PannerHandler::AzimuthElevationDirty | PannerHandler::Dist
anceConeGainDirty | PannerHandler::DopplerRateDirty); | 328 markPannerAsDirty(PannerHandler::AzimuthElevationDirty | PannerHandler::Dist
anceConeGainDirty); |
332 } | 329 } |
333 | 330 |
334 void PannerHandler::setOrientation(float x, float y, float z) | 331 void PannerHandler::setOrientation(float x, float y, float z) |
335 { | 332 { |
336 FloatPoint3D orientation = FloatPoint3D(x, y, z); | 333 FloatPoint3D orientation = FloatPoint3D(x, y, z); |
337 | 334 |
338 if (m_orientation == orientation) | 335 if (m_orientation == orientation) |
339 return; | 336 return; |
340 | 337 |
341 // This synchronizes with process(). | 338 // This synchronizes with process(). |
342 MutexLocker processLocker(m_processLock); | 339 MutexLocker processLocker(m_processLock); |
343 m_orientation = orientation; | 340 m_orientation = orientation; |
344 markPannerAsDirty(PannerHandler::DistanceConeGainDirty); | 341 markPannerAsDirty(PannerHandler::DistanceConeGainDirty); |
345 } | 342 } |
346 | 343 |
347 void PannerHandler::setVelocity(float x, float y, float z) | |
348 { | |
349 FloatPoint3D velocity = FloatPoint3D(x, y, z); | |
350 | |
351 if (m_velocity == velocity) | |
352 return; | |
353 | |
354 // This synchronizes with process(). | |
355 MutexLocker processLocker(m_processLock); | |
356 m_velocity = velocity; | |
357 markPannerAsDirty(PannerHandler::DopplerRateDirty); | |
358 } | |
359 | |
360 void PannerHandler::calculateAzimuthElevation(double* outAzimuth, double* outEle
vation) | 344 void PannerHandler::calculateAzimuthElevation(double* outAzimuth, double* outEle
vation) |
361 { | 345 { |
362 double azimuth = 0.0; | 346 double azimuth = 0.0; |
363 | 347 |
364 // Calculate the source-listener vector | 348 // Calculate the source-listener vector |
365 FloatPoint3D listenerPosition = listener()->position(); | 349 FloatPoint3D listenerPosition = listener()->position(); |
366 FloatPoint3D sourceListener = m_position - listenerPosition; | 350 FloatPoint3D sourceListener = m_position - listenerPosition; |
367 | 351 |
368 // normalize() does nothing if the length of |sourceListener| is zero. | 352 // normalize() does nothing if the length of |sourceListener| is zero. |
369 sourceListener.normalize(); | 353 sourceListener.normalize(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 elevation = 180.0 - elevation; | 389 elevation = 180.0 - elevation; |
406 else if (elevation < -90.0) | 390 else if (elevation < -90.0) |
407 elevation = -180.0 - elevation; | 391 elevation = -180.0 - elevation; |
408 | 392 |
409 if (outAzimuth) | 393 if (outAzimuth) |
410 *outAzimuth = azimuth; | 394 *outAzimuth = azimuth; |
411 if (outElevation) | 395 if (outElevation) |
412 *outElevation = elevation; | 396 *outElevation = elevation; |
413 } | 397 } |
414 | 398 |
415 double PannerHandler::calculateDopplerRate() | |
416 { | |
417 double dopplerShift = 1.0; | |
418 double dopplerFactor = listener()->dopplerFactor(); | |
419 | |
420 if (dopplerFactor > 0.0) { | |
421 double speedOfSound = listener()->speedOfSound(); | |
422 | |
423 const FloatPoint3D& sourceVelocity = m_velocity; | |
424 const FloatPoint3D& listenerVelocity = listener()->velocity(); | |
425 | |
426 // Don't bother if both source and listener have no velocity | |
427 bool sourceHasVelocity = !sourceVelocity.isZero(); | |
428 bool listenerHasVelocity = !listenerVelocity.isZero(); | |
429 | |
430 if (sourceHasVelocity || listenerHasVelocity) { | |
431 // Calculate the source to listener vector | |
432 FloatPoint3D listenerPosition = listener()->position(); | |
433 FloatPoint3D sourceToListener = m_position - listenerPosition; | |
434 | |
435 double sourceListenerMagnitude = sourceToListener.length(); | |
436 | |
437 if (!sourceListenerMagnitude) { | |
438 // Source and listener are at the same position. Skip the comput
ation of the doppler | |
439 // shift, and just return the cached value. | |
440 dopplerShift = m_cachedDopplerRate; | |
441 } else { | |
442 double listenerProjection = sourceToListener.dot(listenerVelocit
y) / sourceListenerMagnitude; | |
443 double sourceProjection = sourceToListener.dot(sourceVelocity) /
sourceListenerMagnitude; | |
444 | |
445 listenerProjection = -listenerProjection; | |
446 sourceProjection = -sourceProjection; | |
447 | |
448 double scaledSpeedOfSound = speedOfSound / dopplerFactor; | |
449 listenerProjection = std::min(listenerProjection, scaledSpeedOfS
ound); | |
450 sourceProjection = std::min(sourceProjection, scaledSpeedOfSound
); | |
451 | |
452 dopplerShift = ((speedOfSound - dopplerFactor * listenerProjecti
on) / (speedOfSound - dopplerFactor * sourceProjection)); | |
453 fixNANs(dopplerShift); // avoid illegal values | |
454 | |
455 // Limit the pitch shifting to 4 octaves up and 3 octaves down. | |
456 if (dopplerShift > 16.0) | |
457 dopplerShift = 16.0; | |
458 else if (dopplerShift < 0.125) | |
459 dopplerShift = 0.125; | |
460 } | |
461 } | |
462 } | |
463 | |
464 return dopplerShift; | |
465 } | |
466 | |
467 float PannerHandler::calculateDistanceConeGain() | 399 float PannerHandler::calculateDistanceConeGain() |
468 { | 400 { |
469 FloatPoint3D listenerPosition = listener()->position(); | 401 FloatPoint3D listenerPosition = listener()->position(); |
470 | 402 |
471 double listenerDistance = m_position.distanceTo(listenerPosition); | 403 double listenerDistance = m_position.distanceTo(listenerPosition); |
472 double distanceGain = m_distanceEffect.gain(listenerDistance); | 404 double distanceGain = m_distanceEffect.gain(listenerDistance); |
473 double coneGain = m_coneEffect.gain(m_position, m_orientation, listenerPosit
ion); | 405 double coneGain = m_coneEffect.gain(m_position, m_orientation, listenerPosit
ion); |
474 | 406 |
475 return float(distanceGain * coneGain); | 407 return float(distanceGain * coneGain); |
476 } | 408 } |
477 | 409 |
478 void PannerHandler::azimuthElevation(double* outAzimuth, double* outElevation) | 410 void PannerHandler::azimuthElevation(double* outAzimuth, double* outElevation) |
479 { | 411 { |
480 ASSERT(context()->isAudioThread()); | 412 ASSERT(context()->isAudioThread()); |
481 | 413 |
482 if (isAzimuthElevationDirty()) { | 414 if (isAzimuthElevationDirty()) { |
483 calculateAzimuthElevation(&m_cachedAzimuth, &m_cachedElevation); | 415 calculateAzimuthElevation(&m_cachedAzimuth, &m_cachedElevation); |
484 m_isAzimuthElevationDirty = false; | 416 m_isAzimuthElevationDirty = false; |
485 } | 417 } |
486 | 418 |
487 *outAzimuth = m_cachedAzimuth; | 419 *outAzimuth = m_cachedAzimuth; |
488 *outElevation = m_cachedElevation; | 420 *outElevation = m_cachedElevation; |
489 } | 421 } |
490 | 422 |
491 double PannerHandler::dopplerRate() | |
492 { | |
493 ASSERT(context()->isAudioThread()); | |
494 | |
495 if (isDopplerRateDirty()) { | |
496 m_cachedDopplerRate = calculateDopplerRate(); | |
497 m_isDopplerRateDirty = false; | |
498 } | |
499 | |
500 return m_cachedDopplerRate; | |
501 } | |
502 | |
503 float PannerHandler::distanceConeGain() | 423 float PannerHandler::distanceConeGain() |
504 { | 424 { |
505 ASSERT(context()->isAudioThread()); | 425 ASSERT(context()->isAudioThread()); |
506 | 426 |
507 if (isDistanceConeGainDirty()) { | 427 if (isDistanceConeGainDirty()) { |
508 m_cachedDistanceConeGain = calculateDistanceConeGain(); | 428 m_cachedDistanceConeGain = calculateDistanceConeGain(); |
509 m_isDistanceConeGainDirty = false; | 429 m_isDistanceConeGainDirty = false; |
510 } | 430 } |
511 | 431 |
512 return m_cachedDistanceConeGain; | 432 return m_cachedDistanceConeGain; |
513 } | 433 } |
514 | 434 |
515 void PannerHandler::markPannerAsDirty(unsigned dirty) | 435 void PannerHandler::markPannerAsDirty(unsigned dirty) |
516 { | 436 { |
517 if (dirty & PannerHandler::AzimuthElevationDirty) | 437 if (dirty & PannerHandler::AzimuthElevationDirty) |
518 m_isAzimuthElevationDirty = true; | 438 m_isAzimuthElevationDirty = true; |
519 | 439 |
520 if (dirty & PannerHandler::DistanceConeGainDirty) | 440 if (dirty & PannerHandler::DistanceConeGainDirty) |
521 m_isDistanceConeGainDirty = true; | 441 m_isDistanceConeGainDirty = true; |
522 | |
523 if (dirty & PannerHandler::DopplerRateDirty) | |
524 m_isDopplerRateDirty = true; | |
525 } | 442 } |
526 | 443 |
527 void PannerHandler::setChannelCount(unsigned long channelCount, ExceptionState&
exceptionState) | 444 void PannerHandler::setChannelCount(unsigned long channelCount, ExceptionState&
exceptionState) |
528 { | 445 { |
529 ASSERT(isMainThread()); | 446 ASSERT(isMainThread()); |
530 AbstractAudioContext::AutoLocker locker(context()); | 447 AbstractAudioContext::AutoLocker locker(context()); |
531 | 448 |
532 // A PannerNode only supports 1 or 2 channels | 449 // A PannerNode only supports 1 or 2 channels |
533 if (channelCount > 0 && channelCount <= 2) { | 450 if (channelCount > 0 && channelCount <= 2) { |
534 if (m_channelCount != channelCount) { | 451 if (m_channelCount != channelCount) { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
608 pannerHandler().setPosition(x, y, z); | 525 pannerHandler().setPosition(x, y, z); |
609 } | 526 } |
610 | 527 |
611 void PannerNode::setOrientation(float x, float y, float z) | 528 void PannerNode::setOrientation(float x, float y, float z) |
612 { | 529 { |
613 pannerHandler().setOrientation(x, y, z); | 530 pannerHandler().setOrientation(x, y, z); |
614 } | 531 } |
615 | 532 |
616 void PannerNode::setVelocity(float x, float y, float z) | 533 void PannerNode::setVelocity(float x, float y, float z) |
617 { | 534 { |
618 pannerHandler().setVelocity(x, y, z); | 535 // The velocity is not used internally and cannot be read back by scripts, |
| 536 // so it can be ignored entirely. |
619 } | 537 } |
620 | 538 |
621 String PannerNode::distanceModel() const | 539 String PannerNode::distanceModel() const |
622 { | 540 { |
623 return pannerHandler().distanceModel(); | 541 return pannerHandler().distanceModel(); |
624 } | 542 } |
625 | 543 |
626 void PannerNode::setDistanceModel(const String& model) | 544 void PannerNode::setDistanceModel(const String& model) |
627 { | 545 { |
628 pannerHandler().setDistanceModel(model); | 546 pannerHandler().setDistanceModel(model); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
682 { | 600 { |
683 return pannerHandler().coneOuterGain(); | 601 return pannerHandler().coneOuterGain(); |
684 } | 602 } |
685 | 603 |
686 void PannerNode::setConeOuterGain(double gain) | 604 void PannerNode::setConeOuterGain(double gain) |
687 { | 605 { |
688 pannerHandler().setConeOuterGain(gain); | 606 pannerHandler().setConeOuterGain(gain); |
689 } | 607 } |
690 | 608 |
691 } // namespace blink | 609 } // namespace blink |
692 | |
OLD | NEW |