Package com.sun.j3d.audioengines.javasound

Source Code of com.sun.j3d.audioengines.javasound.JSDirectionalSample

/*     */ package com.sun.j3d.audioengines.javasound;
/*     */
/*     */ import com.sun.j3d.audioengines.AuralParameters;
/*     */ import javax.media.j3d.Transform3D;
/*     */ import javax.vecmath.Point3f;
/*     */ import javax.vecmath.Vector3f;
/*     */
/*     */ class JSDirectionalSample extends JSPositionalSample
/*     */ {
/*  31 */   Vector3f xformDirection = new Vector3f(0.0F, 0.0F, 1.0F);
/*     */
/*     */   void setXformedDirection()
/*     */   {
/*  42 */     if (!getVWrldXfrmFlag())
/*     */     {
/*  45 */       this.xformDirection.set(this.direction);
/*     */     }
/*     */     else
/*     */     {
/*  50 */       this.vworldXfrm.transform(this.direction, this.xformDirection);
/*     */     }
/*     */   }
/*     */
/*     */   double intersectEllipse(double max, double min)
/*     */   {
/* 161 */     Vector3f xAxis = this.direction;
/*     */
/* 163 */     Vector3f sourceToHead = this.sourceToCenterEar;
/*     */
/* 165 */     if ((xAxis == null) || (sourceToHead == null))
/*     */     {
/* 168 */       return -1.0D;
/*     */     }
/*     */
/* 172 */     double dotProduct = sourceToHead.dot(xAxis) / (sourceToHead.length() * xAxis.length());
/*     */
/* 177 */     double theta = (float)Math.acos(dotProduct);
/*     */
/* 184 */     double minPlusMax = min + max;
/* 185 */     double tangent = Math.tan(theta);
/* 186 */     double xSquared = 1.0D / (4.0D / (minPlusMax * minPlusMax) + tangent * tangent / (min * max));
/*     */
/* 189 */     double x = Math.sqrt(xSquared);
/*     */
/* 198 */     double y = tangent * x;
/*     */
/* 201 */     double ySquared = y * y;
/*     */
/* 206 */     float distance = (float)Math.sqrt(xSquared + ySquared);
/*     */
/* 209 */     return distance;
/*     */   }
/*     */
/*     */   float findFactor(double distanceToHead, double[] maxDistanceArray, float[] maxFactorArray, double[] minDistanceArray, float[] minFactorArray)
/*     */   {
/* 245 */     if ((minDistanceArray == null) || (minFactorArray == null))
/*     */     {
/* 250 */       return findFactor(distanceToHead, maxDistanceArray, maxFactorArray);
/*     */     }
/*     */
/* 257 */     if ((maxDistanceArray == null) || (maxFactorArray == null))
/*     */     {
/* 260 */       return -1.0F;
/*     */     }
/*     */
/* 263 */     int arrayLength = maxDistanceArray.length;
/* 264 */     if (arrayLength < 2)
/*     */     {
/* 267 */       return -1.0F;
/*     */     }
/* 269 */     int largestIndex = arrayLength - 1;
/*     */
/* 277 */     if (distanceToHead >= maxDistanceArray[largestIndex])
/*     */     {
/* 287 */       return maxFactorArray[largestIndex];
/*     */     }
/*     */
/* 294 */     if (distanceToHead <= minDistanceArray[0])
/*     */     {
/* 301 */       return minFactorArray[0];
/*     */     }
/*     */
/* 309 */     double[] distanceArray = new double[arrayLength];
/* 310 */     float[] factorArray = new float[arrayLength];
/* 311 */     boolean[] intersectionCalculated = new boolean[arrayLength];
/*     */
/* 313 */     for (int i = 0; i < arrayLength; i++)
/* 314 */       intersectionCalculated[i] = false;
/* 315 */     boolean intersectionOnEllipse = false;
/* 316 */     int factorIndex = -1;
/*     */
/* 331 */     int lowIndex = 0;
/* 332 */     int highIndex = largestIndex;
/*     */
/* 336 */     while (lowIndex < highIndex - 1)
/*     */     {
/* 345 */       if (intersectionCalculated[lowIndex] == 0) {
/* 346 */         distanceArray[lowIndex] = intersectEllipse(maxDistanceArray[lowIndex], minDistanceArray[lowIndex]);
/*     */
/* 349 */         if (distanceArray[lowIndex] >= 0.0D) {
/* 350 */           intersectionCalculated[lowIndex] = true;
/*     */         }
/*     */         else
/*     */         {
/* 356 */           distanceArray[lowIndex] = ((minDistanceArray[lowIndex] + maxDistanceArray[lowIndex]) * 0.5D);
/*     */
/* 364 */           intersectionCalculated[lowIndex] = true;
/*     */         }
/*     */       }
/*     */
/* 368 */       if (intersectionCalculated[highIndex] == 0) {
/* 369 */         distanceArray[highIndex] = intersectEllipse(maxDistanceArray[highIndex], minDistanceArray[highIndex]);
/*     */
/* 372 */         if (distanceArray[highIndex] >= 0.0D) {
/* 373 */           intersectionCalculated[highIndex] = true;
/*     */         }
/*     */         else
/*     */         {
/* 379 */           distanceArray[highIndex] = ((minDistanceArray[highIndex] + maxDistanceArray[highIndex]) * 0.5D);
/*     */
/* 387 */           intersectionCalculated[highIndex] = true;
/*     */         }
/*     */
/*     */       }
/*     */
/* 396 */       if (distanceArray[lowIndex] >= distanceToHead) {
/* 397 */         if ((lowIndex != 0) && (distanceToHead < distanceArray[lowIndex]));
/* 410 */         intersectionOnEllipse = true;
/* 411 */         factorIndex = lowIndex;
/* 412 */         break;
/*     */       }
/* 414 */       if (distanceArray[highIndex] <= distanceToHead) {
/* 415 */         if ((highIndex != largestIndex) && (distanceToHead > distanceArray[highIndex]));
/* 428 */         intersectionOnEllipse = true;
/* 429 */         factorIndex = highIndex;
/* 430 */         break;
/*     */       }
/*     */
/* 433 */       if ((distanceToHead > distanceArray[lowIndex]) && (distanceToHead < distanceArray[highIndex]))
/*     */       {
/* 435 */         int indexMid = lowIndex + (highIndex - lowIndex) / 2;
/* 436 */         if (distanceToHead <= distanceArray[indexMid])
/*     */         {
/* 438 */           highIndex = indexMid;
/*     */         }
/* 440 */         else lowIndex = indexMid;
/*     */
/*     */       }
/*     */
/*     */     }
/*     */
/* 452 */     if ((intersectionOnEllipse) && (factorIndex >= 0))
/*     */     {
/* 463 */       double returnValue = (distanceArray[factorIndex] - minDistanceArray[factorIndex]) / (maxDistanceArray[factorIndex] - minDistanceArray[factorIndex]) * (maxFactorArray[factorIndex] - minFactorArray[factorIndex]) + minFactorArray[factorIndex];
/*     */
/* 474 */       return (float)returnValue;
/*     */     }
/*     */
/* 484 */     double highFactorValue = 1.0D;
/* 485 */     double lowFactorValue = 0.0D;
/* 486 */     highFactorValue = (distanceArray[highIndex] - minDistanceArray[highIndex]) / (maxDistanceArray[highIndex] - minDistanceArray[highIndex]) * (maxFactorArray[highIndex] - minFactorArray[highIndex]) + minFactorArray[highIndex];
/*     */
/* 502 */     lowFactorValue = (distanceArray[lowIndex] - minDistanceArray[lowIndex]) / (maxDistanceArray[lowIndex] - minDistanceArray[lowIndex]) * (maxFactorArray[lowIndex] - minFactorArray[lowIndex]) + minFactorArray[lowIndex];
/*     */
/* 537 */     double returnValue = (distanceToHead - distanceArray[lowIndex]) / (distanceArray[highIndex] - distanceArray[lowIndex]) * (highFactorValue - lowFactorValue) + factorArray[lowIndex];
/*     */
/* 545 */     return (float)returnValue;
/*     */   }
/*     */
/*     */   float calculateDistanceAttenuation(float distance)
/*     */   {
/* 558 */     float factor = findFactor(distance, this.attenuationDistance, this.attenuationGain, this.backAttenuationDistance, this.backAttenuationGain);
/*     */
/* 561 */     if (factor < 0.0F) {
/* 562 */       return 1.0F;
/*     */     }
/* 564 */     return factor;
/*     */   }
/*     */
/*     */   float calculateAngularGain()
/*     */   {
/* 573 */     float angle = findAngularOffset();
/* 574 */     float factor = findFactor(angle, this.angularDistance, this.angularGain);
/* 575 */     if (factor < 0.0F) {
/* 576 */       return 1.0F;
/*     */     }
/* 578 */     return factor;
/*     */   }
/*     */
/*     */   float findAngularOffset()
/*     */   {
/* 597 */     Vector3f unitToEar = new Vector3f();
/* 598 */     Vector3f unitDirection = new Vector3f();
/* 599 */     Point3f xformPosition = this.positions[this.currentIndex];
/* 600 */     Point3f xformCenterEar = this.centerEars[this.currentIndex];
/*     */
/* 608 */     unitToEar.x = (xformCenterEar.x - xformPosition.x);
/* 609 */     unitToEar.y = (xformCenterEar.y - xformPosition.y);
/* 610 */     unitToEar.z = (xformCenterEar.z - xformPosition.z);
/* 611 */     unitToEar.normalize();
/* 612 */     unitDirection.normalize(this.direction);
/* 613 */     float dotProduct = unitToEar.dot(unitDirection);
/* 614 */     float angle = (float)Math.acos(dotProduct);
/*     */
/* 617 */     return angle;
/*     */   }
/*     */
/*     */   void calculateFilter(float distance, AuralParameters attribs)
/*     */   {
/* 639 */     float distanceFilter = 44100.0F;
/* 640 */     float angularFilter = 44100.0F;
/* 641 */     int arrayLength = attribs.getDistanceFilterLength();
/* 642 */     int filterType = attribs.getDistanceFilterType();
/*     */
/* 644 */     boolean distanceFilterFound = false;
/* 645 */     boolean angularFilterFound = false;
/* 646 */     if ((filterType == -1) && (arrayLength > 0)) {
/* 647 */       double[] distanceArray = new double[arrayLength];
/* 648 */       float[] cutoffArray = new float[arrayLength];
/* 649 */       attribs.getDistanceFilter(distanceArray, cutoffArray);
/*     */
/* 658 */       float angle = findAngularOffset();
/* 659 */       distanceFilter = findFactor(angle, this.angularDistance, this.angularFilterCutoff);
/*     */
/* 661 */       if (distanceFilter < 0.0F)
/* 662 */         distanceFilterFound = false;
/*     */       else
/* 664 */         distanceFilterFound = true;
/*     */     }
/*     */     else {
/* 667 */       distanceFilterFound = false;
/* 668 */       distanceFilter = -1.0F;
/*     */     }
/*     */
/* 675 */     arrayLength = this.angularDistance.length;
/* 676 */     filterType = this.angularFilterType;
/* 677 */     if ((filterType != -1) && (arrayLength > 0)) {
/* 678 */       angularFilter = findFactor(distance, this.angularDistance, this.angularFilterCutoff);
/*     */
/* 680 */       if (angularFilter < 0.0F)
/* 681 */         angularFilterFound = false;
/*     */       else
/* 683 */         angularFilterFound = true;
/*     */     }
/*     */     else {
/* 686 */       angularFilterFound = false;
/* 687 */       angularFilter = -1.0F;
/*     */     }
/*     */
/* 690 */     this.filterFlag = ((distanceFilterFound) || (angularFilterFound));
/* 691 */     if (distanceFilter < 0.0F)
/* 692 */       this.filterFreq = angularFilter;
/* 693 */     else if (angularFilter < 0.0F)
/* 694 */       this.filterFreq = distanceFilter;
/*     */     else
/* 696 */       this.filterFreq = Math.min(distanceFilter, angularFilter);
/*     */   }
/*     */ }

/* Location:           Z:\System\Library\Java\Extensions\j3daudio.jar
* Qualified Name:     com.sun.j3d.audioengines.javasound.JSDirectionalSample
* JD-Core Version:    0.6.2
*/
TOP

Related Classes of com.sun.j3d.audioengines.javasound.JSDirectionalSample

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.