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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
|
/*
* Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*
*/
package javax.media.j3d;
import javax.vecmath.Point2f;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;
/**
* A ConeSoundRetained node defines a point sound source located at some
* location
* in space whose amplitude is constrained not only by maximum and minimum
* amplitude
* spheres but by two concentric cone volumes directed down an vector radiating
* from the sound's location.
*/
class ConeSoundRetained extends PointSoundRetained {
/**
* The Cone Sound's direction vector. This is the cone axis.
*/
Vector3f direction = new Vector3f(0.0f, 0.0f, 1.0f);
// The transformed direction of this sound
Vector3f xformDirection = new Vector3f(0.0f, 0.0f, 1.0f);
// Sound's gain is attenuated for listener locations off-angle from
// the source source direction.
// This can be set of three numbers:
// angular distance in radians
// gain scale factor
// filtering (currently the only filtering supported is lowpass)
// For now the only supported filterType will be LOW_PASS frequency cutoff.
// At some time full FIR filtering will be supported.
static final int NO_FILTERING = -1;
static final int LOW_PASS = 1;
// Pairs of distances and gain scale factors that define piecewise linear
// gain BACK attenuation between each pair.
// These are used for defining elliptical attenuation regions.
float[] backAttenuationDistance = null;
float[] backAttenuationGain = null;
float[] angularDistance = {0.0f, ((float)(Math.PI) * 0.5f)};
float[] angularGain = {1.0f, 0.0f};
int filterType = NO_FILTERING;
float[] frequencyCutoff = {Sound.NO_FILTER, Sound.NO_FILTER};
ConeSoundRetained() {
this.nodeType = NodeRetained.CONESOUND;
}
// *********************
//
// Distance Gain methods
//
// *********************
/**
* Sets this sound's distance gain elliptical attenuation -
* where gain scale factor is applied to sound based on distance listener
* is from sound source.
* @param frontAttenuation defined by pairs of (distance,gain-scale-factor)
* @param backAttenuation defined by pairs of (distance,gain-scale-factor)
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
void setDistanceGain(Point2f[] frontAttenuation,
Point2f[] backAttenuation ) {
this.setDistanceGain(frontAttenuation);
this.setBackDistanceGain(backAttenuation);
}
/**
* Sets this sound's distance gain attenuation as an array of Point2fs.
* @param frontDistance array of monotonically-increasing floats
* @param frontGain array of non-negative scale factors
* @param backDistance array of monotonically-increasing floats
* @param backGain array of non-negative scale factors
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
void setDistanceGain(float[] frontDistance, float[] frontGain,
float[] backDistance, float[] backGain) {
this.setDistanceGain(frontDistance, frontGain);
this.setBackDistanceGain(backDistance, backGain);
}
/**
* Sets this sound's back distance gain attenuation - where gain scale
* factor is applied to sound based on distance listener along the negative
* sound direction axis from sound source.
* @param attenuation defined by pairs of (distance,gain-scale-factor)
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
void setBackDistanceGain(Point2f[] attenuation)
{
// if attenuation array null set both attenuation components to null
if (attenuation == null) {
this.backAttenuationDistance = null;
this.backAttenuationGain = null;
}
else {
int attenuationLength = attenuation.length;
if (attenuationLength == 0) {
this.backAttenuationDistance = null;
this.backAttenuationGain = null;
}
else {
this.backAttenuationDistance = new float[attenuationLength];
this.backAttenuationGain = new float[attenuationLength];
for (int i = 0; i < attenuationLength; i++) {
this.backAttenuationDistance[i] = attenuation[i].x;
this.backAttenuationGain[i] = attenuation[i].y;
}
}
}
dispatchAttribChange(BACK_DISTANCE_GAIN_DIRTY_BIT, attenuation);
if (source != null && source.isLive()) {
notifySceneGraphChanged(false);
}
}
/**
* Sets this sound's back distance gain attenuation as an array of Point2fs.
* @param distance array of monotonically-increasing floats
* @param gain array of non-negative scale factors
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
void setBackDistanceGain(float[] distance, float[] gain)
{
int distanceLength = 0;
// if distance or gain arrays are null then treat both as null
if (distance == null || gain == null) {
this.backAttenuationDistance = null;
this.backAttenuationGain = null;
}
else {
// now process the back attenuation values
int gainLength = gain.length;
distanceLength = distance.length;
if (distanceLength == 0 || gainLength == 0) {
this.backAttenuationDistance = null;
this.backAttenuationGain = null;
}
else {
this.backAttenuationDistance = new float[distanceLength];
this.backAttenuationGain = new float[distanceLength];
// Copy the distance array into nodes field
System.arraycopy(distance, 0, this.backAttenuationDistance,
0, distanceLength);
// Copy the gain array an array of same length as the distance array
if (distanceLength <= gainLength) {
System.arraycopy(gain, 0, this.backAttenuationGain,
0, distanceLength);
}
else {
System.arraycopy(gain, 0, this.backAttenuationGain, 0, gainLength);
// Extend gain array to length of distance array
// replicate last gain values.
for (int i=gainLength; i< distanceLength; i++) {
this.backAttenuationGain[i] = gain[gainLength - 1];
}
}
}
}
Point2f [] attenuation = new Point2f[distanceLength];
for (int i=0; i<distanceLength; i++) {
attenuation[i] = new Point2f(this.backAttenuationDistance[i],
this.backAttenuationGain[i]);
}
dispatchAttribChange(BACK_DISTANCE_GAIN_DIRTY_BIT, attenuation);
if (source != null && source.isLive()) {
notifySceneGraphChanged(false);
}
}
/**
* Gets this sound's elliptical distance attenuation
* @param frontAttenuation arrays containing forward distances attenuation pairs
* @param backAttenuation arrays containing backward distances attenuation pairs
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
void getDistanceGain(Point2f[] frontAttenuation,
Point2f[] backAttenuation) {
this.getDistanceGain(frontAttenuation);
this.getBackDistanceGain(backAttenuation);
}
/**
* Gets this sound's elliptical distance gain attenuation values in separate arrays
* @param frontDistance array of float distances along the sound axis
* @param fronGain array of non-negative scale factors associated with front distances
* @param backDistance array of float negative distances along the sound axis
* @param backGain array of non-negative scale factors associated with back distances
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
void getDistanceGain(float[] frontDistance, float[] frontGain,
float[] backDistance, float[] backGain) {
this.getDistanceGain( frontDistance, frontGain);
this.getBackDistanceGain( backDistance, backGain);
}
/**
* Retieves sound's back distance attenuation
* Put the contents of the two separate distance and gain arrays into
* an array of Point2f.
* @param attenuation containing distance attenuation pairs
*/
void getBackDistanceGain(Point2f[] attenuation) {
// Write into arrays passed in, don't do a new
if (attenuation == null)
return;
if (this.backAttenuationDistance == null ||
this.backAttenuationGain == null)
return;
// These two array length should be the same
// can assume lengths are non-zero
int distanceLength = this.backAttenuationDistance.length;
int attenuationLength = attenuation.length;
if (distanceLength < attenuationLength)
distanceLength = attenuationLength;
for (int i=0; i< distanceLength; i++) {
attenuation[i].x = this.backAttenuationDistance[i];
attenuation[i].y = this.backAttenuationGain[i];
}
}
/**
* Retieves this sound's back attenuation distance and gain arrays,
* returned in separate arrays.
* @param distance array of monotonically-increasing floats.
* @param gain array of amplitude scale factors associated with distances.
*/
void getBackDistanceGain(float[] distance, float[] gain) {
// write into arrays passed in, don't do a new
if (distance == null || gain == null)
return;
if (this.backAttenuationDistance == null ||
this.backAttenuationGain == null)
return;
// backAttenuationDistance and backAttenuationGain array length should
// be the same
// can assume length is non-zero
int attenuationLength = this.backAttenuationDistance.length;
int distanceLength = distance.length;
if (attenuationLength > distanceLength)
attenuationLength = distanceLength;
System.arraycopy(this.backAttenuationDistance, 0, distance, 0, attenuationLength);
attenuationLength = this.backAttenuationGain.length;
int gainLength = gain.length;
if (attenuationLength > gainLength)
attenuationLength = gainLength;
System.arraycopy(this.backAttenuationGain, 0, gain, 0, attenuationLength);
}
// *********************
//
// Direction Methods
//
// *********************
/**
* Sets this sound's direction from the vector provided.
* @param direction the new direction
*/
void setDirection(Vector3f direction) {
if (staticTransform != null) {
staticTransform.transform.transform(direction, this.direction);
} else {
this.direction.set(direction);
}
dispatchAttribChange(DIRECTION_DIRTY_BIT,
(new Vector3f(this.direction)));
if (source != null && source.isLive()) {
notifySceneGraphChanged(false);
}
}
/**
* Sets this sound's direction from the three values provided.
* @param x the new x direction
* @param y the new y direction
* @param z the new z direction
*/
void setDirection(float x, float y, float z) {
direction.x = x;
direction.y = y;
direction.z = z;
if (staticTransform != null) {
staticTransform.transform.transform(direction);
}
dispatchAttribChange(DIRECTION_DIRTY_BIT, (new Vector3f(direction)));
if (source != null && source.isLive()) {
notifySceneGraphChanged(false);
}
}
/**
* Retrieves this sound's direction and places it in the
* vector provided.
* @return direction vector (axis of cones)
*/
void getDirection(Vector3f direction)
{
if (staticTransform != null) {
Transform3D invTransform = staticTransform.getInvTransform();
invTransform.transform(this.direction, direction);
} else {
direction.set(this.direction);
}
}
void getXformDirection(Vector3f direction)
{
direction.set(this.xformDirection);
}
// ***************************
//
// Angular Attenuation
//
// ***************************
/**
* Sets this sound's angular gain attenuation (not including filter)
* @param attenuation array containing angular distance and gain
*/
void setAngularAttenuation(Point2f[] attenuation) {
int attenuationLength = 0;
this.filterType = NO_FILTERING;
if (attenuation == null) {
this.angularDistance = null;
this.angularGain = null;
}
else {
attenuationLength = attenuation.length;
if (attenuationLength == 0) {
this.angularDistance = null;
this.angularGain = null;
}
else {
this.angularDistance = new float[attenuationLength];
this.angularGain = new float[attenuationLength];
for (int i = 0; i < attenuationLength; i++) {
this.angularDistance[i] = attenuation[i].x;
this.angularGain[i] = attenuation[i].y;
}
} // lengths not zero
} // arrays not null
Point3f [] attenuation3f = new Point3f[attenuationLength];
for (int i=0; i<attenuationLength; i++) {
attenuation3f[i] = new Point3f(this.angularDistance[i],
this.angularGain[i],
Sound.NO_FILTER);
}
dispatchAttribChange(ANGULAR_ATTENUATION_DIRTY_BIT, attenuation3f);
if (source != null && source.isLive()) {
notifySceneGraphChanged(false);
}
}
/**
* Sets this sound's angular attenuation including both gain and filter.
* @param attenuation array containing angular distance, gain and filter
*/
void setAngularAttenuation(Point3f[] attenuation) {
if (attenuation == null) {
this.angularDistance = null;
this.angularGain = null;
this.frequencyCutoff = null;
this.filterType = NO_FILTERING;
}
else {
int attenuationLength = attenuation.length;
if (attenuationLength == 0) {
this.angularDistance = null;
this.angularGain = null;
this.frequencyCutoff = null;
this.filterType = NO_FILTERING;
}
else {
this.angularDistance = new float[attenuationLength];
this.angularGain = new float[attenuationLength];
this.frequencyCutoff = new float[attenuationLength];
this.filterType = LOW_PASS;
for (int i = 0; i < attenuationLength; i++) {
this.angularDistance[i] = attenuation[i].x;
this.angularGain[i] = attenuation[i].y;
this.frequencyCutoff[i] = attenuation[i].z;
}
} // lengths not zero
} // arrays not null
dispatchAttribChange(ANGULAR_ATTENUATION_DIRTY_BIT, attenuation);
if (source != null && source.isLive()) {
notifySceneGraphChanged(false);
}
}
/**
* Sets angular attenuation including gain and filter using separate arrays
* @param distance array containing angular distance
* @param filter array containing angular low-pass frequency cutoff values
*/
void setAngularAttenuation(float[] distance, float[] gain, float[] filter) {
int distanceLength = 0;
if (distance == null || gain == null || filter == null) {
this.angularDistance = null;
this.angularGain = null;
this.frequencyCutoff = null;
this.filterType = NO_FILTERING;
}
else {
distanceLength = distance.length;
int gainLength = gain.length;
if (distanceLength == 0 || gainLength == 0) {
this.angularDistance = null;
this.angularGain = null;
this.frequencyCutoff = null;
this.filterType = NO_FILTERING;
}
else {
int filterLength = filter.length;
this.angularDistance = new float[distanceLength];
this.angularGain = new float[distanceLength];
this.frequencyCutoff = new float[distanceLength];
// Copy the distance array into nodes field
System.arraycopy(distance, 0, this.angularDistance, 0, distanceLength);
// Copy the gain array an array of same length as the distance array
if (distanceLength <= gainLength) {
System.arraycopy(gain, 0, this.angularGain, 0, distanceLength);
}
else {
System.arraycopy(gain, 0, this.angularGain, 0, gainLength);
/**
* Extend gain array to length of distance array by
* replicate last gain values.
*/
for (int i=gainLength; i< distanceLength; i++) {
this.angularGain[i] = gain[gainLength - 1];
}
}
// Copy the filter array an array of same length as the distance array
if (filterLength == 0)
this.filterType = NO_FILTERING;
else {
this.filterType = LOW_PASS;
if (distanceLength <= filterLength) {
System.arraycopy(filter, 0, this.frequencyCutoff,0, distanceLength);
}
else {
System.arraycopy(filter, 0, this.frequencyCutoff, 0, filterLength);
// Extend filter array to length of distance array by
// replicate last filter values.
for (int i=filterLength; i< distanceLength; i++) {
this.frequencyCutoff[i] = filter[filterLength - 1];
}
}
}
} // length not zero
} // arrays not null
Point3f [] attenuation = new Point3f[distanceLength];
for (int i=0; i<distanceLength; i++) {
if (this.filterType != NO_FILTERING) {
attenuation[i] = new Point3f(this.angularDistance[i],
this.angularGain[i],
this.frequencyCutoff[i]);
}
else {
attenuation[i] = new Point3f(this.angularDistance[i],
this.angularGain[i],
Sound.NO_FILTER);
}
}
dispatchAttribChange(ANGULAR_ATTENUATION_DIRTY_BIT, attenuation);
if (source != null && source.isLive()) {
notifySceneGraphChanged(false);
}
}
/**
* Retrieves angular attenuation array length.
* All arrays are forced to same size
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
int getAngularAttenuationLength() {
if (angularDistance == null)
return 0;
else
return (this.angularDistance.length);
}
/**
* Retrieves angular attenuation including gain and filter in a single array
* @param attenuation applied to gain when listener is between cones
*/
void getAngularAttenuation(Point3f[] attenuation) {
/// use attenuation array allocated by user - don't new it
// The three angular attenuation arrays length should be the same
if (this.angularDistance == null || this.angularGain == null)
return;
if (attenuation == null)
return;
int distanceLength = this.angularDistance.length;
if (attenuation.length < distanceLength)
distanceLength = attenuation.length;
for (int i=0; i< distanceLength; i++) {
attenuation[i].x = this.angularDistance[i];
attenuation[i].y = this.angularGain[i];
if (filterType == NO_FILTERING || this.frequencyCutoff == null)
attenuation[i].z = Sound.NO_FILTER;
else if (filterType == LOW_PASS)
attenuation[i].z = this.frequencyCutoff[i];
}
}
/**
* Retrieves angular attenuation including gain and filter
* returned as separate arrays
* @param distance array containing angular distance
* @param gain array containing angular gain attenuation
* @param filter array containing angular low-pass frequency cutoff values
*/
void getAngularAttenuation(float[] distance, float[] gain, float[] filter) {
// use attenuation array allocated by user - don't new it
if (distance == null || gain == null || filter == null)
return;
if (this.angularDistance == null || this.angularGain == null)
return;
int distanceLength = this.angularDistance.length;
if (distance.length < distanceLength)
distanceLength = distance.length;
System.arraycopy(this.angularDistance, 0, distance, 0, distanceLength);
int gainLength = this.angularGain.length;
if (gain.length < gainLength)
gainLength = gain.length;
System.arraycopy(this.angularGain, 0, gain, 0, gainLength);
int filterLength = 0;
if (this.frequencyCutoff == null || filterType == NO_FILTERING)
filterLength = filter.length;
else {
filterLength = this.frequencyCutoff.length;
if (filter.length < filterLength)
filterLength = filter.length;
}
if (filterType == NO_FILTERING || this.frequencyCutoff == null) {
for (int i=0; i< filterLength; i++)
filter[i] = Sound.NO_FILTER;
}
if (filterType == LOW_PASS) {
System.arraycopy(this.frequencyCutoff, 0, filter,0, filterLength);
}
}
/**
* This updates the Direction fields of cone sound.
*
* Neither Angular gain Attenuation and Filtering fields, nor
* back distance gain not maintained in mirror object
*/
@Override
void updateMirrorObject(Object[] objs) {
if (debugFlag)
debugPrint("PointSoundRetained:updateMirrorObj()");
int component = ((Integer)objs[1]).intValue();
int numSnds = ((Integer)objs[2]).intValue();
SoundRetained[] mSnds = (SoundRetained[]) objs[3];
if (component == -1) {
// update every field
initMirrorObject(((ConeSoundRetained)objs[2]));
return;
}
if ((component & DIRECTION_DIRTY_BIT) != 0) {
for (int i = 0; i < numSnds; i++) {
ConeSoundRetained cone = (ConeSoundRetained)mSnds[i];
cone.direction = (Vector3f)objs[4];
cone.getLastLocalToVworld().transform(cone.direction,
cone.xformDirection);
cone.xformDirection.normalize();
}
}
// call the parent's mirror object update routine
super.updateMirrorObject(objs);
}
synchronized void initMirrorObject(ConeSoundRetained ms) {
super.initMirrorObject(ms);
ms.direction.set(this.direction);
ms.xformDirection.set(this.xformDirection);
}
// Called on the mirror object
@Override
void updateTransformChange() {
Transform3D lastLocalToVworld = getLastLocalToVworld();
super.updateTransformChange();
lastLocalToVworld.transform(direction, xformDirection);
xformDirection.normalize();
// set flag looked at by Scheduler to denote Transform change
// this flag will force resneding transformed direction to AudioDevice
if (debugFlag)
debugPrint("ConeSound xformDirection is (" + xformDirection.x + ", "
+ xformDirection.y + ", "+ xformDirection.z + ")");
}
@Override
void mergeTransform(TransformGroupRetained xform) {
super.mergeTransform(xform);
xform.transform.transform(direction);
}
}
|