All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javafx.scene.media.AudioEqualizer Maven / Gradle / Ivy

There is a newer version: 24-ea+15
Show newest version
/*
 * Copyright (c) 2010, 2022, Oracle and/or its affiliates. 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.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

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
     * any change to the internal list can be vetoed
     * if a newly added instance is not valid.
     *
     * 

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

*
    *
  • {@link EqualizerBand#centerFrequencyProperty EqualizerBand.centerFrequency} > 0
  • *
  • {@link EqualizerBand#bandwidthProperty EqualizerBand.bandwidth} > 0
  • *
  • {@link EqualizerBand#MIN_GAIN EqualizerBand.MIN_GAIN} ≤ * {@link EqualizerBand#gainProperty EqualizerBand.gain} ≤ * {@link EqualizerBand#MAX_GAIN EqualizerBand.MAX_GAIN}
  • *
* *

The default set of bands is as in the following table; all bands have * unity gain (0 dB). *

* * * * * * * * * * * * *
AudioEqualizer Band Table
Band IndexCenter Frequency (Hz)Bandwidth (Hz)
03219
16439
212578
3250156
4500312
51000625
620001250
740002500
880005000
91600010000
* * @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."); } } } } private BooleanProperty enabled; public final void setEnabled(boolean value) { enabledProperty().set(value); } public final boolean isEnabled() { return enabled == null ? false : enabled.get(); } /** * Enables or disables AudioEqualizer. If the enabled property * is set to {@code false}, {@code 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. * * @defaultValue {@code true} * @return the enabled property */ 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."); } } } } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy