summaryrefslogtreecommitdiffstats
path: root/src/classes/share/com/sun/j3d/audioengines/AudioEngine3D.java
blob: 09ff33260617453d8d69575ddafed5e6fad99b42 (plain)
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
/*
 * 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.
 *
 */

package com.sun.j3d.audioengines;

import java.util.ArrayList;

import javax.media.j3d.AudioDevice3D;
import javax.media.j3d.MediaContainer;
import javax.media.j3d.PhysicalEnvironment;
import javax.media.j3d.Transform3D;
import javax.media.j3d.View;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;


/**
 * The AudioEngine3D Class defines an audio output device that generates
 * sound 'image' from high-level sound parameters passed to it during
 * scene graph.
 *
 * <P>
 * The methods in this class are meant to be optionally overridden by an
 * extended class.  This extended class would provice device specific code.
 *
 * <P>
 * Error checking on all parameters passed to these methods is already
 * explicitly being done by the Java 3D core code that calls these methods.
 *
 * <p>
 * NOTE: AudioEngine developers should not subclass this class directly.
 * Subclass AudioEngine3DL2 instead.
 */

public abstract class AudioEngine3D extends AudioEngine implements AudioDevice3D
{
    /*
     *  Identifiers of sample associated with sound source
     *  This array grows as the AudioDevice3D implementation requires it larger.
     */
    protected ArrayList samples = new ArrayList(64);

    /**
     *  Current View sound is being rendered
     */
    protected View  currentView = (View)null;

    /*
     * current Aural attribute Parameters
     */
    protected AuralParameters attribs = new AuralParameters();

    /**
     * Construct a new AudioEngine with the specified PhysicalEnvironment.
     * @param physicalEnvironment the physical environment object where we
     * want access to this device.
     */
    public AudioEngine3D(PhysicalEnvironment physicalEnvironment ) {
        super(physicalEnvironment);
    }

    /*
     *
     * Methods that affect AudioEngine3D fields that are NOT associated
     * with a specific sound sample
     *
     */

    /**
     * Save a reference to the current View object.
     * @param reference to current view object
     */
    public void  setView(View reference) {
        currentView = reference;
        return;
    }
    /**
     * Get reference to the current View object.
     * @return reference to current view object
     */
    public View getView() {
        return (currentView);
    }

    /*
     *
     *  Methods explicitly affect sound rendering and that require
     *  audio device specific methods that override this class.
     *
     */

    /**
     * Prepare Sound in device.
     * Makes sound assessible to device - in this case attempts to load sound
     * Stores sound type and data.
     * @param soundType denotes type of sound: Background, Point or Cone
     * @param soundData descrition of sound source data
     * @return index into sample vector of Sample object for sound
     */
    public int   prepareSound(int soundType, MediaContainer soundData) {
        // This method must be overridden by device specific implementation
        return Sample.NULL_SAMPLE;
    }

    /**
     * Clear Sound.
     * Removes/clears associated sound data with this sound source node
     * @param index device specific reference number to device driver sample
     */
    public abstract void clearSound(int index);

    /**
     * Set the transform for local to virtual world coordinate space
     * @param index device specific reference number to device driver sample
     * @param trans is a reference to virtual world composite transform
     */
    public void  setVworldXfrm(int index, Transform3D trans) {
         Sample sample = getSample(index);
         if (sample != null)
             sample.vworldXfrm.set(trans);
         return;
    }
    /**
     * Start sample playing on audio device
     * @param index device specific reference number to device driver sample
     * @return status: < 0 denotes an error
     */
    public abstract int startSample(int index);

    /**
     * Stop sample playing on audio device
     * @param index device specific reference number to device driver sample
     * @return status: < 0 denotes an error
     */
    public abstract int stopSample(int index);

    /**
     * Update sample.
     * Implies that some parameters affecting rendering have been modified.
     * @param index device specific reference number to device driver sample
     */
    // TODO: The update method exists on a TEMPORARY basis.
    public abstract void updateSample(int index);

    /**
     * Mute sample.
     * @param index device specific reference number to device driver sample
     */
    public abstract void muteSample(int index);

    /**
     * Unmute sample.
     * @param index device specific reference number to device driver sample
     */
    public abstract void unmuteSample(int index);

    /**
     * Pause sample.
     * @param index device specific reference number to device driver sample
     */
    public abstract void pauseSample(int index);

    /**
     * Unpause sample.
     * @param index device specific reference number to device driver sample
     */
    public abstract void unpauseSample(int index);

    /*
     *
     *  Methods that affect fields associated with the sound sample
     *  and that may cause implicit rendering.
     *
     */
    /**
     * Set gain scale factor applied to sample.
     * @param index device specific reference number to device driver sample
     * @param scaleFactor floating point multiplier applied to sample amplitude
     */
    public void  setSampleGain(int index, float scaleFactor) {
        Sample sample = getSample(index);
        if (sample != null)
            sample.setGain(scaleFactor);
        return;
    }

    /**
     * Set number of times sample is looped.
     * @param index device specific reference number to device driver sample
     * @param count number of times sample is repeated
     */
    public void   setLoop(int index, int count) {
        Sample sample = getSample(index);
        if (sample != null)
            sample.setLoopCount(count);
        return;
    }

    /**
     * Set location of sample.
     * @param index device specific reference number to device driver sample
     * @param position point location in virtual world coordinate of sample
     */
    public void  setPosition(int index, Point3d position) {
        Sample sample = getSample(index);
        if (sample != null)
            sample.setPosition(position);
        return;
    }

    /* Set elliptical distance attenuation arrays applied to sample amplitude.
     * @param index device specific reference number to device driver sample
     * @param frontDistance defines an array of distance along the position axis
     * thru which ellipses pass
     * @param frontAttenuationScaleFactor gain scale factors
     * @param backDistance defines an array of distance along the negative axis
     * thru which ellipses pass
     * @param backAttenuationScaleFactor gain scale factors
     */
    public void setDistanceGain(int index,
              double[] frontDistance, float[] frontAttenuationScaleFactor,
              double[] backDistance, float[] backAttenuationScaleFactor) {
        Sample sample = getSample(index);
        if (sample != null)
            sample.setDistanceGain(frontDistance, frontAttenuationScaleFactor,
                                   backDistance, backAttenuationScaleFactor);
        return;
    }

    /**
     * Set direction vector of sample.
     * @param index device specific reference number to device driver sample
     * @param direction vector in virtual world coordinate.
     */
    public void setDirection(int index, Vector3d direction) {
        Sample sample = getSample(index);
        if (sample != null)
            sample.setDirection(direction);
        return;
    }

    /**
     * Set angular attenuation arrays affecting angular amplitude attenuation
     * and angular distance filtering.
     * @param index device specific reference number to device driver sample
     * @param filterType denotes type of filtering (on no filtering) applied
     * to sample.
     * @param angle array containing angular distances from sound axis
     * @param attenuationScaleFactor array containing gain scale factor
     * @param filterCutoff array containing filter cutoff frequencies.
     * The filter values for each tuples can be set to Sound.NO_FILTER.
     */
    public void setAngularAttenuation(int index, int filterType,
          double[] angle, float[] attenuationScaleFactor, float[] filterCutoff) {
        Sample sample = getSample(index);
        if (sample != null)
            sample.setAngularAttenuation(filterType, angle,
                       attenuationScaleFactor, filterCutoff);
        return;
    }

    /**
     * Set rolloff value for current aural attribute applied to all samples.
     * @param rolloff scale factor applied to standard speed of sound.
     */
    public void setRolloff(float rolloff) {
        attribs.rolloff = rolloff;
        return;
    }

    /**
     * Set reverberation surface reflection coefficient value for current aural
     * attribute applied to all samples.
     * @param coefficient applied to amplitude of reverbation added at each
     * iteration of reverb processing.
     */
    public void setReflectionCoefficient(float coefficient) {
        attribs.reflectionCoefficient = coefficient;
        return;
    }

    /**
     * Set reverberation delay time for current aural attribute applied to
     * all samples.
     * @param reverbDelay amount of time in millisecond between each
     * iteration of reverb processing.
     */
    public void setReverbDelay(float reverbDelay) {
        attribs.reverbDelay = reverbDelay;
        return;
    }

    /**
     * Set reverberation order for current aural attribute applied to all
     * samples.
     * @param reverbOrder number of times reverb process loop is iterated.
     */
    public void setReverbOrder(int reverbOrder) {
        attribs.reverbOrder = reverbOrder;
        return;
    }

    /**
     * Set distance filter for current aural attribute applied to all samples.
     * @param filterType denotes type of filtering (on no filtering) applied
     * to all sample based on distance between listener and sound.
     * @param dist is an attenuation array of distance and low-pass filter values.
     */
    public void setDistanceFilter(int filterType,
              double[] dist, float[]  filterCutoff) {
        attribs.setDistanceFilter(filterType, dist, filterCutoff);
        return;
    }

    /**
     * Set frequency scale factor for current aural attribute applied to all
     * samples.
     * @param scaleFactor frequency scale factor applied to samples normal
     * playback rate.
     */
    public void setFrequencyScaleFactor(float scaleFactor) {
        attribs.frequencyScaleFactor = scaleFactor;
        return;
    }
    /**
     * Set velocity scale factor for current aural attribute applied to all
     * samples when Doppler is calculated.
     * @param scaleFactor scale factor applied to postional samples'
     * listener-to-soundSource velocity.
     * playback rate.
     */
    public void setVelocityScaleFactor(float scaleFactor) {
        attribs.velocityScaleFactor = scaleFactor;
        return;
    }

    /**
     * Get number of channels used by a particular sample on the audio device.
     * @param index device specific reference number to device driver sample
     * @return number of channels currently being used by this sample.
     */
    public int    getNumberOfChannelsUsed(int index) {
        // This method must be overridden by device specific implementation
        Sample sample = getSample(index);
        if (sample != null)
            return (sample.getNumberOfChannelsUsed());
        else
            return 0;
    }

    /**
     * Get number of channels that would be used by a particular sample on
     * the audio device given the mute flag passed in as a parameter.
     * @param index device specific reference number to device driver sample
     * @param muteFlag denotes the mute state to assume while executing this
     * query. This mute value does not have to match the current mute state
     * of the sample.
     * @return number of channels that would be used by this sample if it
     * were playing.
     */
    public int    getNumberOfChannelsUsed(int index, boolean muteFlag) {
        // This method must be overridden by device specific implementation
        Sample sample = getSample(index);
        if (sample != null)
            return (sample.getNumberOfChannelsUsed());
        else
            return 0;
    }

    /**
     * Get length of time a sample would play if allowed to play to completion.
     * @param index device specific reference number to device driver sample
     * @return length of sample in milliseconds
     */
    public long  getSampleDuration(int index) {
        Sample sample = getSample(index);
        if (sample != null)
            return (sample.getDuration());
        else
            return 0L;
    }

    /**
     * Get time this sample begun playing on the audio device.
     * @param index device specific reference number to device driver sample
     * @return system clock time sample started
     */
    public long  getStartTime(int index) {
        Sample sample = getSample(index);
        if (sample != null)
            return (sample.getStartTime());
        else
            return 0L;
    }

    /**
     * Get reference to the array list of samples
     * @return reference to samples list
     * @deprecated unsafe to get reference to samples list with this method.
     * It's better to directly reference samples list within a synchronized
     * block which also contains calls to .getSample(index).
     */
    protected ArrayList getSampleList() {
        return (samples);
    }

    public int getSampleListSize() {
        return (samples.size());
    }

    /**
     * Get specific sample from indexed sample list
     * Checks for valid index before attempting to get sample from list.
     * @param index device specific reference number to device driver sample
     * @return reference to sample; returns null if index out of range.
     *
     * @since Java 3D 1.2.1
     */
    public Sample getSample(int index) {
        synchronized(samples) {
            if ((index >= 0) && (index < samples.size())) {
                Sample sample = (Sample)samples.get(index);
                return (sample);
            }
            else
                return null;
        }
    }

    /*
     * Get reference to current aural attribute parameters associated with
     * this audio device.
     * @return reference to current aural attribute parameters
     */
    public AuralParameters getAuralParameters() {
        return (attribs);
    }
}