/* * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * * * * * * * * * * * * * * * * * * * */ package javafx.scene.media; import java.util.List; import javafx.beans.property.BooleanProperty; import javafx.beans.property.BooleanPropertyBase; import javafx.collections.ObservableList; import javafx.collections.FXCollections; import com.sun.javafx.collections.VetoableListDecorator; import com.sun.media.jfxmedia.logging.Logger; /** * The AudioEqualizer class provides audio equalization control for * a media player. It contains an {@link ObservableList} of {@link EqualizerBand} * elements. Each AudioEqualizer instance is connected to a * {@link MediaPlayer} and may be obtained using the * {@link MediaPlayer#getAudioEqualizer MediaPlayer.getAudioEqualizer} method. * * @see MediaPlayer * @see EqualizerBand * @since JavaFX 2.0 */ public final class AudioEqualizer { /** * Maximum number of bands an AudioEqualizer may contain. * In the current implementation this value is 64. */ public static final int MAX_NUM_BANDS = com.sun.media.jfxmedia.effects.AudioEqualizer.MAX_NUM_BANDS; private com.sun.media.jfxmedia.effects.AudioEqualizer jfxEqualizer = null; private final ObservableList bands; private final Object disposeLock = new Object(); /** * ObservableList containing {@link EqualizerBand} elements. The content of * the sequence may be changed by adding or removing {@link EqualizerBand} * elements. When adding elements, the user must be prepared to catch * {@link IllegalArgumentException}s because the internal list is a * {@link VetoableObservableList} and any change to the list can be vetoed * if a newly added instance is not valid. * *

The constraints for a valid {@link EqualizerBand} instance are: *

*

* *

The default set of bands is as in the following table; all bands have * unity gain (0 dB). * * * * * * * * * * * * *
Band IndexCenter Frequency (Hz)Bandwidth (Hz)
03219
16439
212578
3250156
4500312
51000625
620001250
740002500
880005000
91600010000
* * @throws IllegalArgumentException * @return ObservableList containing {@link EqualizerBand} elements. */ public final ObservableList getBands() { return bands; } AudioEqualizer() { bands = new Bands(); // Add reasonable bands bands.addAll(new EqualizerBand(32, 19, 0), new EqualizerBand(64, 39, 0), new EqualizerBand(125, 78, 0), new EqualizerBand(250, 156, 0), new EqualizerBand(500, 312, 0), new EqualizerBand(1000, 625, 0), new EqualizerBand(2000, 1250, 0), new EqualizerBand(4000, 2500, 0), new EqualizerBand(8000, 5000, 0), new EqualizerBand(16000, 10000, 0)); } /** * Called by NativePlayer when native part is ready * * @param jfxEqualizer - Instance of native equalizer */ void setAudioEqualizer(com.sun.media.jfxmedia.effects.AudioEqualizer jfxEqualizer) { synchronized (disposeLock) { if (this.jfxEqualizer == jfxEqualizer) { return; } if (this.jfxEqualizer != null && jfxEqualizer == null) { this.jfxEqualizer.setEnabled(false); for (EqualizerBand band : bands) { band.setJfxBand(null); } this.jfxEqualizer = null; return; } this.jfxEqualizer = jfxEqualizer; // Propogate enabled jfxEqualizer.setEnabled(isEnabled()); // Propogate bands for (EqualizerBand band : bands) { if (band.getCenterFrequency() > 0 && band.getBandwidth() > 0) { com.sun.media.jfxmedia.effects.EqualizerBand jfxBand = jfxEqualizer.addBand(band.getCenterFrequency(), band.getBandwidth(), band.getGain()); // setJfxBand will throw an NPE if jfxBand is null which // should never happen. band.setJfxBand(jfxBand); } else { Logger.logMsg(Logger.ERROR, "Center frequency [" + band.getCenterFrequency() + "] and bandwidth [" + band.getBandwidth() + "] must be greater than 0."); } } } } /** * Whether the AudioEqualizer is enabled. The default value is * true. */ private BooleanProperty enabled; /** * Enables or disables AudioEqualizer. If the enabled property * is set to false, {@link AudioEqualizer} settings are preserved but * not taken into account during playback, which is equivalent to setting all * {@link EqualizerBand#gainProperty EqualizerBand.gain} properties to zero. */ public final void setEnabled(boolean value) { enabledProperty().set(value); } /** * Return enabled state of the AudioEqualizer. * @return true if AudioEqulizer is enabled, false otherwise. */ public final boolean isEnabled() { return enabled == null ? false : enabled.get(); } public BooleanProperty enabledProperty() { if (enabled == null) { enabled = new BooleanPropertyBase() { @Override protected void invalidated() { synchronized (disposeLock) { if (jfxEqualizer != null) { jfxEqualizer.setEnabled(enabled.get()); } } } @Override public Object getBean() { return AudioEqualizer.this; } @Override public String getName() { return "enabled"; } }; } return enabled; } private class Bands extends VetoableListDecorator { public Bands() { super(FXCollections.observableArrayList()); } @Override protected void onProposedChange(List toBeAdded, int[] toBeRemoved) { synchronized (disposeLock) { if (jfxEqualizer != null) { for (int i = 0; i < toBeRemoved.length; i += 2) { for (EqualizerBand band : subList(toBeRemoved[i], toBeRemoved[i + 1])) { jfxEqualizer.removeBand(band.getCenterFrequency()); } } for (EqualizerBand band : toBeAdded) { if (band.getCenterFrequency() > 0 && band.getBandwidth() > 0) { com.sun.media.jfxmedia.effects.EqualizerBand jfxBand = jfxEqualizer.addBand(band.getCenterFrequency(), band.getBandwidth(), band.getGain()); band.setJfxBand(jfxBand); } else { Logger.logMsg(Logger.ERROR, "Center frequency [" + band.getCenterFrequency() + "] and bandwidth [" + band.getBandwidth() + "] must be greater than 0."); } } } } } } }