📄 jsthread.java
字号:
/* * $RCSfile: JSThread.java,v $ * * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or * maintenance of any nuclear facility. * * $Revision: 1.4 $ * $Date: 2007/02/09 17:20:03 $ * $State: Exp $ */package com.sun.j3d.audioengines.javasound;/* * JavaSound engine Thread * * IMPLEMENTATION NOTE: The JavaSoundMixer is incomplete and really needs * to be rewritten. When this is done, we may or may not need this class. */import javax.media.j3d.*;import com.sun.j3d.audioengines.*;/** * The Thread Class extended for JavaSound Mixer specific audio device * calls that dynamically, in 'real-time" change engine parameters * such as volume/gain and sample-rate/frequency(pitch). */class JSThread extends com.sun.j3d.audioengines.AudioEngineThread { /** * The thread data for this thread */ int totalChannels = 0; /** * flags denoting if dynamic gain or rate interpolation is to be performed */ boolean rampGain = false; // global thread flat rampRate set true only when setTargetRate called // for any sample. but it is cleared only by doWork when no sample // has a need for the rate to be ramped any further. boolean rampRate = false;/*** TODO: * * scalefactors applied to current sample rate to determine delta changes * in rate (in Hz) * float currentGain = 1.0f; float targetGain = 1.0f;***********/ // reference to engine that created this thread AudioEngine3D audioEngine = null; /** * This constructor simply assigns the given id. */ JSThread(ThreadGroup t, AudioEngine3DL2 engine) { super(t, "J3D-JavaSoundThread"); audioEngine = engine; // TODO: really get total JavaSound channels totalChannels = 32; if (debugFlag) debugPrint("JSThread.constructor("+t+")"); } /** * This method performs one iteration of pending work to do * * Wildly "garbled" sounds was caused by unequal changes in delta * time verses delta distances (resulting in jumps in rate factors * calculated for Doppler. This work thread is meant to smoothly * increment/decrement changes in rate (and other future parameters) * until the target value is reached. */ synchronized public void doWork() { if (debugFlag) debugPrint("JSThread.doWork()");/******* while (rampRate || rampGain) {*********//****** DESIGN// Loop while sound is playing, reget attributes and gains/reverb,... params// update lowlevel params then read modify then copy to line(s)can keep my own loop count for streams??? not really*******/ // QUESTION: will size ever get smaller after get performed??? int numSamples = audioEngine.getSampleListSize(); JSSample sample = null; int numRateRamps = 0; for (int index = 0; index < numSamples; index++) { // loop thru samples looking for ones needing rate incremented sample = (JSSample)audioEngine.getSample(index); if (sample == null) continue; if (sample.getRampRateFlag()) { if (debugFlag) debugPrint(" rampRate true"); boolean endOfRampReached = adjustRate(sample); sample.setRampRateFlag(!endOfRampReached); if (!endOfRampReached) numRateRamps++; } // TODO: support changes in gain this way as well } if (numRateRamps > 0) { rampRate = true;runMonitor(RUN, 0, null); } else rampRate = false;/********* try { Thread.sleep(4); } catch (InterruptedException e){}*********//******** } // while*********/ // otherwise do nothing } int getTotalChannels() { return (totalChannels); } /** * Gradually change rate scale factor * * If the rate change is too great suddenly, it sounds like a * jump, so we need to change gradually over time. * Since an octive delta change up is 2.0 but down is 0.5, forced * "max" rate of change is different for both. * @return true if target rate value was reached */ boolean adjustRate(JSSample sample) { // QUESTION: what should max delta rate changes be // Using 1/32 of a half-step (1/12 of an octive)??? double maxRateChangeDown = 0.00130213; double maxRateChangeUp = 0.00260417; double lastActualRateRatio = sample.getCurrentRateRatio(); double requestedRateRatio = sample.getTargetRateRatio(); boolean endOfRamp = false; // flag denotes if target rate reached if ( lastActualRateRatio > 0 ) { double sampleRateRatio = requestedRateRatio; // in case diff = 0 double diff = 0.0; if (debugFlag) { debugPrint("JSThread.adjustRate: between " + lastActualRateRatio + " & " + requestedRateRatio); } diff = requestedRateRatio - lastActualRateRatio; if (diff > 0.0) { // direction of movement is towards listener // inch up towards the requested target rateRatio if (diff >= maxRateChangeUp) { sampleRateRatio = lastActualRateRatio + maxRateChangeUp; if (debugFlag) { debugPrint(" adjustRate: " + "diff >= maxRateChangeUp so "); debugPrint(" adjustRate: " + " sampleRateRatio incremented up by max"); } endOfRamp = false; // target value not reached } /* * otherwise delta change is within tolerance * so use requested RateRatio as calculated w/out change */ else { sampleRateRatio = requestedRateRatio; if (debugFlag) { debugPrint(" adjustRate: " + " requestedRateRatio reached"); } endOfRamp = true; // reached } } else if (diff < 0.0) { // movement is away from listener // inch down towards the requested target rateRatio if ((-diff) >= maxRateChangeDown) { sampleRateRatio = lastActualRateRatio - maxRateChangeDown; if (debugFlag) { debugPrint(" adjustRate: " + "-(diff) >= maxRateChangeUp so "); debugPrint(" adjustRate: " + " sampleRateRatio incremented down by max "); } endOfRamp = false; // target value not reached } /* * otherwise negitive delta change is within tolerance so * use sampleRateRatio as calculated w/out change */ else { sampleRateRatio = requestedRateRatio; if (debugFlag) { debugPrint(" adjustRate: " + " requestedRateRatio reached"); } endOfRamp = true; // reached } } else // there is no difference between last set and requested rates return true; this.setSampleRate(sample, (float)sampleRateRatio); } else { // this is the first time thru with a rate change if (debugFlag) { debugPrint(" adjustRate: " + "last requested rateRatio not set yet " + "so sampleRateRatio left unchanged"); } this.setSampleRate(sample, (float)requestedRateRatio); endOfRamp = false; // target value not reached } return endOfRamp; } // adjustRate void setSampleRate(JSSample sample, JSAuralParameters attribs) {// TODO: } // gain set at start sample time as well void setSampleGain(JSSample sample, JSAuralParameters attribs) {/******* // take fields as already set in sample and updates gain // called after sample.render performed if (debugFlag) debugPrint("JSThread.setSampleGain()");leftGain, rightGain if (debugFlag) { debugPrint(" " + "StereoGain during update " + leftGain + ", " + rightGain); debugPrint(" " + "StereoDelay during update " + leftDelay + ", " + rightDelay); } int dataType = sample.getDataType(); int soundType = sample.getSoundType(); boolean muted = sample.getMuteFlag(); if (debugFlag) debugPrint("setStereoGain for sample "+sample+" " + leftGain +
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -