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
|
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.impl.neomedia;
import java.awt.*;
import java.util.*;
import java.util.List;
import javax.media.*;
import net.java.sip.communicator.service.neomedia.*;
import net.java.sip.communicator.service.neomedia.event.*;
import net.java.sip.communicator.util.*;
/**
* Controls media service volume input or output. If a playback volume level
* is set we change it on all current players, as we synchronize volume
* on all players. Implements interface exposed from media service, also
* implements the interface used in the Renderer part of JMF and merges the two
* functionalities to work as one.
*
* @author Damian Minkov
* @author Lyubomir Marinov
*/
public class AbstractVolumeControl
implements VolumeControl,
GainControl
{
/**
* The <tt>Logger</tt> used by the <tt>VolumeControlImpl</tt> class and
* its instances for logging output.
*/
private static final Logger logger
= Logger.getLogger(AbstractVolumeControl.class);
/**
* The minimum volume level we can handle.
*/
private static final float MIN_VOLUME_LEVEL = 0.0F;
/**
* The maximum volume level we can handle.
*/
private static final float MAX_VOLUME_LEVEL = 1.0F;
/**
* The default volume level.
*/
private static final float DEFAULT_VOLUME_LEVEL = 0.5F;
/**
* The <tt>VolumeChangeListener</tt> interested in volume change events
* through VolumeControl Interface.
*/
private final List<VolumeChangeListener> volumeChangeListeners
= new ArrayList<VolumeChangeListener>();
/**
* Listeners interested in volume change inside jmf.
*/
private List<GainChangeListener> gainChangeListeners;
/**
* The current volume level.
*/
private float volumeLevel = DEFAULT_VOLUME_LEVEL;
/**
* Current mute state, by default we start unmuted.
*/
private boolean mute = false;
/**
* Current level in db.
*/
private float db;
/**
* The initial volume level, when this instance was created.
*/
private final float initialVolumeLevel;
/**
* The name of the configuration property which specifies the value of the
* volume level of this <tt>AbstractVolumeControl</tt>.
*/
private final String volumeLevelConfigurationPropertyName;
/**
* Creates volume control instance and initializes initial level value
* if stored in the configuration service.
*
* @param volumeLevelConfigurationPropertyName the name of the configuration
* property which specifies the value of the volume level of the new
* instance
*/
public AbstractVolumeControl(
String volumeLevelConfigurationPropertyName)
{
this.volumeLevelConfigurationPropertyName
= volumeLevelConfigurationPropertyName;
// read initial level from config service if any
String initialVolumeLevelString
= NeomediaActivator.getConfigurationService().getString(
this.volumeLevelConfigurationPropertyName);
float initialVolumeLevel = DEFAULT_VOLUME_LEVEL;
try
{
if (initialVolumeLevelString != null)
{
initialVolumeLevel = Float.parseFloat(initialVolumeLevelString);
if(logger.isDebugEnabled())
{
logger.debug(
"Restored volume: " + initialVolumeLevelString);
}
}
}
catch(Throwable t)
{
logger.warn("Error restoring volume", t);
}
this.initialVolumeLevel = initialVolumeLevel;
this.volumeLevel = this.initialVolumeLevel;
}
/**
* Applies the gain specified by <tt>gainControl</tt> to the signal defined
* by the <tt>length</tt> number of samples given in <tt>buffer</tt>
* starting at <tt>offset</tt>.
*
* @param gainControl the <tt>GainControl</tt> which specifies the gain to
* apply
* @param buffer the samples of the signal to apply the gain to
* @param offset the start of the samples of the signal in <tt>buffer</tt>
* @param length the number of samples of the signal given in
* <tt>buffer</tt>
*/
public static void applyGain(
GainControl gainControl,
byte[] buffer, int offset, int length)
{
if (gainControl.getMute())
Arrays.fill(buffer, offset, offset + length, (byte) 0);
else
{
// Assign the maximum of 200% to the volume scale.
float level = gainControl.getLevel() * 2;
if (level != 1)
{
for (int i = offset, toIndex = offset + length;
i < toIndex;
i += 2)
{
int i1 = i + 1;
short s = (short) ((buffer[i] & 0xff) | (buffer[i1] << 8));
/* Clip, don't wrap. */
int si = s;
si = (int) (si * level);
if (si > Short.MAX_VALUE)
s = Short.MAX_VALUE;
else if (si < Short.MIN_VALUE)
s = Short.MIN_VALUE;
else
s = (short) si;
buffer[i] = (byte) s;
buffer[i1] = (byte) (s >> 8);
}
}
}
}
/**
* Current volume value.
*
* @return the current volume level.
*
* @see net.java.sip.communicator.service.neomedia.VolumeControl
*/
public float getVolume()
{
return volumeLevel;
}
/**
* Get the current gain set for this
* object as a value between 0.0 and 1.0
*
* @return The gain in the level scale (0.0-1.0).
*
* @see javax.media.GainControl
*/
public float getLevel()
{
return volumeLevel;
}
/**
* Returns the minimum allowed volume value.
*
* @return the minimum allowed volume value.
*
* @see net.java.sip.communicator.service.neomedia.VolumeControl
*/
public float getMinValue()
{
return MIN_VOLUME_LEVEL;
}
/**
* Returns the maximum allowed volume value.
*
* @return the maximum allowed volume value.
*
* @see net.java.sip.communicator.service.neomedia.VolumeControl
*/
public float getMaxValue()
{
return MAX_VOLUME_LEVEL;
}
/**
* Changes volume level.
*
* @param value the new level to set.
* @return the actual level which was set.
*
* @see net.java.sip.communicator.service.neomedia.VolumeControl
*/
public float setVolume(float value)
{
return this.setVolumeLevel(value);
}
/**
* Set the gain using a floating point scale
* with values between 0.0 and 1.0.
* 0.0 is silence; 1.0 is the loudest
* useful level that this <code>GainControl</code> supports.
*
* @param level The new gain value specified in the level scale.
* @return The level that was actually set.
*
* @see javax.media.GainControl
*/
public float setLevel(float level)
{
return this.setVolumeLevel(level);
}
/**
* Internal implementation combining setting level from JMF
* and from outside Media Service.
*
* @param value the new value, changed if different from current
* volume settings.
* @return the value that was changed or just the current one if
* the same.
*/
private float setVolumeLevel(float value)
{
if (volumeLevel == value)
return value;
if(value < MIN_VOLUME_LEVEL)
volumeLevel = MIN_VOLUME_LEVEL;
else if(value > MAX_VOLUME_LEVEL)
volumeLevel = MAX_VOLUME_LEVEL;
else
volumeLevel = value;
fireVolumeChange();
// save the level change, so we can restore it on next run
NeomediaActivator.getConfigurationService().setProperty(
this.volumeLevelConfigurationPropertyName,
String.valueOf(volumeLevel));
float f1 = value / initialVolumeLevel;
db = (float)((Math.log((double)f1 != 0.0D ?
f1
: 0.0001D) / Math.log(10D)) * 20D);
fireGainEvents();
return volumeLevel;
}
/**
* Mutes current sound.
*
* @param mute mutes/unmutes.
*/
public void setMute(boolean mute)
{
if (this.mute != mute)
{
this.mute = mute;
fireVolumeChange();
fireGainEvents();
}
}
/**
* Get mute state of sound.
*
* @return mute state of sound.
*/
public boolean getMute()
{
return mute;
}
/**
* Set the gain in decibels.
* Setting the gain to 0.0 (the default) implies that the audio
* signal is neither amplified nor attenuated.
* Positive values amplify the audio signal and negative values attenuate
* the signal.
*
* @param gain The new gain in dB.
* @return The gain that was actually set.
*
* @see javax.media.GainControl
*/
public float setDB(float gain)
{
if(this.db != gain)
{
this.db = gain;
float f1 = (float)Math.pow(10D, (double)this.db / 20D);
float volumeLevel = f1 * this.initialVolumeLevel;
if(volumeLevel < 0.0F)
volumeLevel = 0.0F;
else if(volumeLevel > 1.0F)
volumeLevel = 1.0F;
setVolumeLevel(volumeLevel);
}
return this.db;
}
/**
* Get the current gain set for this object in dB.
* @return The gain in dB.
*/
public float getDB()
{
return this.db;
}
/**
* Register for gain change update events.
* A <code>GainChangeEvent</code> is posted when the state
* of the <code>GainControl</code> changes.
*
* @param listener The object to deliver events to.
*/
public void addGainChangeListener(GainChangeListener listener)
{
if(listener != null)
{
if(gainChangeListeners == null)
gainChangeListeners = new ArrayList<GainChangeListener>();
gainChangeListeners.add(listener);
}
}
/**
* Remove interest in gain change update events.
*
* @param listener The object that has been receiving events.
*/
public void removeGainChangeListener(GainChangeListener listener)
{
if(listener != null && gainChangeListeners != null)
gainChangeListeners.remove(listener);
}
/**
* Adds a <tt>VolumeChangeListener</tt> to be informed for any change
* in the volume levels.
*
* @param listener volume change listener.
*/
public void addVolumeChangeListener(VolumeChangeListener listener)
{
synchronized(volumeChangeListeners)
{
if(!volumeChangeListeners.contains(listener))
volumeChangeListeners.add(listener);
}
}
/**
* Removes a <tt>VolumeChangeListener</tt>.
*
* @param listener the volume change listener to be removed.
*/
public void removeVolumeChangeListener(VolumeChangeListener listener)
{
synchronized(volumeChangeListeners)
{
volumeChangeListeners.remove(listener);
}
}
/**
* Fire a change in volume to interested listeners.
*/
private void fireVolumeChange()
{
VolumeChangeListener[] ls;
synchronized(volumeChangeListeners)
{
ls
= volumeChangeListeners.toArray(
new VolumeChangeListener[volumeChangeListeners.size()]);
}
VolumeChangeEvent changeEvent
= new VolumeChangeEvent(this, volumeLevel, mute);
for(VolumeChangeListener l : ls)
l.volumeChange(changeEvent);
}
/**
* Fire events informing for our current state.
*/
private void fireGainEvents()
{
if(gainChangeListeners != null)
{
GainChangeEvent gainchangeevent
= new GainChangeEvent(this, mute, db, volumeLevel);
for(GainChangeListener gainchangelistener : gainChangeListeners)
gainchangelistener.gainChange(gainchangeevent);
}
}
/**
* Not used.
* @return null
*/
public Component getControlComponent()
{
return null;
}
}
|