📄 jspositionalsample.java
字号:
float velocityScaleFactor = attribs.velocityScaleFactor; if (debugFlag || dopplerFlag) debugPrint("JSPositionalSample: attribs NOT null"); if (rolloff <= 0.0f) { if (debugFlag) debugPrint(" rolloff = " + rolloff + " <= 0.0" ); // TODO: Make sound silent // return ??? } else if (frequencyScaleFactor <= 0.0f) { if (debugFlag) debugPrint(" freqScaleFactor = " + frequencyScaleFactor + " <= 0.0" ); // TODO: Pause sound silent // return ??? } else if (velocityScaleFactor > 0.0f) { if (debugFlag || dopplerFlag) debugPrint(" velocityScaleFactor = " + velocityScaleFactor);/******* if (deltaTime > 0) {*******/ // Doppler can be calculated after the second time // updateXformParams() is executed dopplerRatio = calculateDoppler(attribs); if (dopplerRatio == 0.0f) { // dopplerRatio zeroo denotes no changed // TODO: But what if frequencyScaleFactor has changed if (debugFlag) { debugPrint("JSPositionalSample: render: " + "dopplerRatio returned zero; no change"); } } else if (dopplerRatio == -1.0f) { // error returned by calculateDoppler if (debugFlag) { debugPrint("JSPositionalSample: render: " + "dopplerRatio returned = " + dopplerRatio + "< 0"); } // TODO: Make sound silent // return ??? } else if (dopplerRatio > 0.0f) { // rate could be changed rateRatio = dopplerRatio * frequencyScaleFactor * getRateScaleFactor(); if (debugFlag) { debugPrint(" scaled by frequencyScaleFactor = " + frequencyScaleFactor ); } }/****** } else { if (debugFlag) debugPrint("deltaTime <= 0 - skip Doppler calc"); }******/ } else { // auralAttributes not null but velocityFactor <= 0 // Doppler is disabled rateRatio = frequencyScaleFactor * getRateScaleFactor(); } } /* * since aural attributes undefined, default values are used, * thus no Doppler calculated */ else { if (debugFlag || dopplerFlag) debugPrint("JSPositionalSample: attribs null"); rateRatio = 1.0f; } this.panSample(attribs); } /* ***************** * * Calculate Angular Gain * * *****************/ /* * Calculates the Gain scale factor applied to the overall gain for * a sound based on angle between a sound's projected direction and the * vector between the sounds position and center ear. * * For Point Sounds this value is always 1.0f. */ float calculateAngularGain() { return(1.0f); } /* ***************** * * Calculate Filter * * *****************/ /* * Calculates the low-pass cutoff frequency filter value applied to the * a sound based on both: * Distance Filter (from Aural Attributes) based on distance * between the sound and the listeners position * Angular Filter (for Directional Sounds) based on the angle * between a sound's projected direction and the * vector between the sounds position and center ear. * The lowest of these two filter is used. * This filter value is stored into the sample's filterFreq field. */ void calculateFilter(float distance, AuralParameters attribs) { // setting filter cutoff freq to 44.1kHz which, in this // implementation, is the same as not performing filtering float distanceFilter = 44100.0f; float angularFilter = 44100.0f; int arrayLength = attribs.getDistanceFilterLength(); int filterType = attribs.getDistanceFilterType(); boolean distanceFilterFound = false; boolean angularFilterFound = false; if ((filterType != AuralParameters.NO_FILTERING) && arrayLength > 0) { double[] distanceArray = new double[arrayLength]; float[] cutoffArray = new float[arrayLength]; attribs.getDistanceFilter(distanceArray, cutoffArray); if (debugFlag) { debugPrint("distanceArray cutoffArray"); for (int i=0; i<arrayLength; i++) debugPrint((float)(distanceArray[i]) + ", " + cutoffArray[i]); } distanceFilter = findFactor((double)distance, distanceArray, cutoffArray); if (distanceFilter < 0.0f) distanceFilterFound = false; else distanceFilterFound = true; } else { distanceFilterFound = false; distanceFilter = -1.0f; } if (debugFlag) debugPrint(" calculateFilter arrayLength = " + arrayLength); // Angular filter only applies to directional sound sources. angularFilterFound = false; angularFilter = -1.0f; filterFlag = distanceFilterFound || angularFilterFound; filterFreq = distanceFilter; if (debugFlag) debugPrint(" calculateFilter flag,freq = " + filterFlag + "," + filterFreq ); } /* ***************** * * Find Factor * * *****************/ /* * Interpolates the correct output factor given a 'distance' value * and references to the distance array and factor array used in * the calculation. These array parameters could be either linear or * angular distance arrays, or filter arrays. * The values in the distance array are monotonically increasing. * This method looks at pairs of distance array values to find which * pair the input distance argument is between distanceArray[index] and * distanceArray[index+1]. * The index is used to get factorArray[index] and factorArray[index+1]. * Then the ratio of the 'distance' between this pair of distanceArray * values is used to scale the two found factorArray values proportionally. * The resulting factor is returned, unless there is an error, then -1.0 * is returned. */ float findFactor(double distance, double[] distanceArray, float[] factorArray) { int index, lowIndex, highIndex, indexMid; if (debugFlag) debugPrint("JSPositionalSample.findFactor entered"); /* * Error checking */ if (distanceArray == null || factorArray == null) { if (debugFlag) debugPrint(" findFactor: arrays null"); return -1.0f; // no value } int arrayLength = distanceArray.length; if (arrayLength < 2) { if (debugFlag) debugPrint(" findFactor: arrays length < 2"); return -1.0f; // no value } int largestIndex = arrayLength - 1; /* * Calculate distanceGain scale factor */ if (distance >= distanceArray[largestIndex]) { if (debugFlag) { debugPrint(" findFactor: distance > " + distanceArray[largestIndex]); debugPrint(" distanceArray length = "+ arrayLength); } return factorArray[largestIndex]; } else if (distance <= distanceArray[0]) { if (debugFlag) debugPrint(" findFactor: distance < " + distanceArray[0]); return factorArray[0]; } /* * Distance between points within attenuation array. * Use binary halfing of distance array */ else { lowIndex = 0; highIndex = largestIndex; if (debugFlag) debugPrint(" while loop to find index: "); while (lowIndex < (highIndex-1)) { if (debugFlag) { debugPrint(" lowIndex " + lowIndex + ", highIndex " + highIndex); debugPrint(" d.A. pair for lowIndex " + distanceArray[lowIndex] + ", " + factorArray[lowIndex] ); debugPrint(" d.A. pair for highIndex " + distanceArray[highIndex] + ", " + factorArray[highIndex] ); } /* * we can assume distance is between distance atttenuation vals * distanceArray[lowIndex] and distanceArray[highIndex] * calculate gain scale factor based on distance */ if (distanceArray[lowIndex] >= distance) { if (distance < distanceArray[lowIndex]) { if (internalErrors) debugPrint("Internal Error: binary halving in " + " findFactor failed; distance < index value"); } if (debugFlag) { debugPrint( " index == distanceGain " + lowIndex); debugPrint(" findFactor returns [LOW=" + lowIndex + "] " + factorArray[lowIndex]); } // take value of scale factor directly from factorArray return factorArray[lowIndex]; } else if (distanceArray[highIndex] <= distance) { if (distance > distanceArray[highIndex]) { if (internalErrors) debugPrint("Internal Error: binary halving in " + " findFactor failed; distance > index value"); } if (debugFlag) { debugPrint( " index == distanceGain " + highIndex); debugPrint(" findFactor returns [HIGH=" + highIndex + "] " + factorArray[highIndex]); } // take value of scale factor directly from factorArray return factorArray[highIndex]; } if (distance > distanceArray[lowIndex] && distance < distanceArray[highIndex] ) { indexMid = lowIndex + ((highIndex - lowIndex) / 2); if (distance <= distanceArray[indexMid]) // value of distance in lower "half" of list highIndex = indexMid; else // value if distance in upper "half" of list lowIndex = indexMid; } } /* of while */ /* * ratio: distance from listener to sound source * between lowIndex and highIndex times * attenuation value between lowIndex and highIndex * gives linearly interpolationed attenuation value */ if (debugFlag) { debugPrint( " ratio calculated using lowIndex " + lowIndex + ", highIndex " + highIndex); debugPrint( " d.A. pair for lowIndex " + distanceArray[lowIndex]+", "+factorArray[lowIndex] ); debugPrint( " d.A. pair for highIndex " + distanceArray[highIndex]+", "+factorArray[highIndex] ); } float outputFactor = ((float)(((distance - distanceArray[lowIndex])/ (distanceArray[highIndex] - distanceArray[lowIndex]) ) ) * (factorArray[highIndex] - factorArray[lowIndex]) ) + factorArray[lowIndex] ; if (debugFlag) debugPrint(" findFactor returns " + outputFactor); return outputFactor; } } /** * CalculateDistanceAttenuation * * Simply calls generic (for PointSound) 'findFactor()' with * a single set of attenuation distance and gain scale factor arrays. */ float calculateDistanceAttenuation(float distance) { float factor = 1.0f; factor = findFactor((double)distance, this.attenuationDistance, this.attenuationGain); if (factor >= 0.0) return (factor); else return (1.0f); } /* ****************** * * Pan Sample * * ******************/ /* * Sets pan and delay for a single sample associated with this Sound. * Front and Back quadrants are treated the same. */ void panSample(AuralParameters attribs) { int quadrant = 1; float intensityHigh = 1.0f;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -