| /* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| package chartaudioarea; |
| |
| import javafx.application.Application; |
| import javafx.scene.Group; |
| import javafx.scene.Scene; |
| import javafx.scene.chart.AreaChart; |
| import javafx.scene.chart.NumberAxis; |
| import javafx.scene.chart.XYChart; |
| import javafx.scene.media.AudioSpectrumListener; |
| import javafx.scene.media.Media; |
| import javafx.scene.media.MediaPlayer; |
| import javafx.stage.Stage; |
| |
| /** |
| * An advanced area chart with sound and animation. The sound is in AAC audio format. |
| * |
| * @see javafx.scene.chart.AreaChart |
| * @see javafx.scene.chart.Chart |
| * @see javafx.scene.chart.NumberAxis |
| * @see javafx.scene.chart.XYChart |
| * @see javafx.scene.media.AudioSpectrumListener |
| * @see javafx.scene.media.Media |
| * @see javafx.scene.media.MediaPlayer |
| */ |
| public class ChartAudioArea extends Application { |
| private XYChart.Data<Number,Number>[] series1Data; |
| private AudioSpectrumListener audioSpectrumListener; |
| |
| private static final String AUDIO_URI = System.getProperty("demo.audio.url","http://download.oracle.com/otndocs/javafx/JavaRap_Audio.mp4"); |
| private static MediaPlayer audioMediaPlayer; |
| private static final boolean PLAY_AUDIO = Boolean.parseBoolean(System.getProperty("demo.play.audio","true")); |
| |
| private void init(Stage primaryStage) { |
| Group root = new Group(); |
| primaryStage.setScene(new Scene(root)); |
| root.getChildren().add(createChart()); |
| audioSpectrumListener = new AudioSpectrumListener() { |
| @Override public void spectrumDataUpdate(double timestamp, double duration, |
| float[] magnitudes, float[] phases) { |
| for (int i = 0; i < series1Data.length; i++) { |
| series1Data[i].setYValue(magnitudes[i] + 60); |
| } |
| } |
| }; |
| } |
| |
| public void play() { |
| this.startAudio(); |
| } |
| |
| @Override public void stop() { |
| this.stopAudio(); |
| } |
| |
| protected AreaChart<Number,Number> createChart() { |
| final NumberAxis xAxis = new NumberAxis(0,128,8); |
| final NumberAxis yAxis = new NumberAxis(0,50,10); |
| final AreaChart<Number,Number> ac = new AreaChart<Number,Number>(xAxis,yAxis); |
| // setup chart |
| ac.setId("audioAreaDemo"); |
| ac.setLegendVisible(false); |
| ac.setTitle("Live Audio Spectrum Data"); |
| ac.setAnimated(false); |
| xAxis.setLabel("Frequency Bands"); |
| yAxis.setLabel("Magnitudes"); |
| yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis,null,"dB")); |
| // add starting data |
| XYChart.Series<Number,Number> series = new XYChart.Series<Number,Number>(); |
| series.setName("Audio Spectrum"); |
| //noinspection unchecked |
| series1Data = new XYChart.Data[(int)xAxis.getUpperBound()]; |
| for (int i=0; i<series1Data.length; i++) { |
| series1Data[i] = new XYChart.Data<Number,Number>(i,50); |
| series.getData().add(series1Data[i]); |
| } |
| ac.getData().add(series); |
| return ac; |
| } |
| |
| private void startAudio() { |
| if (PLAY_AUDIO) { |
| getAudioMediaPlayer().setAudioSpectrumListener(audioSpectrumListener); |
| getAudioMediaPlayer().play(); |
| } |
| } |
| |
| private void stopAudio() { |
| if (getAudioMediaPlayer().getAudioSpectrumListener() == audioSpectrumListener) { |
| getAudioMediaPlayer().pause(); |
| } |
| } |
| |
| private static MediaPlayer getAudioMediaPlayer() { |
| if (audioMediaPlayer == null) { |
| Media audioMedia = new Media(AUDIO_URI); |
| audioMediaPlayer = new MediaPlayer(audioMedia); |
| } |
| return audioMediaPlayer; |
| } |
| |
| @Override public void start(Stage primaryStage) throws Exception { |
| init(primaryStage); |
| primaryStage.show(); |
| play(); |
| } |
| |
| /** |
| * The main() method is ignored in correctly deployed JavaFX |
| * application. main() serves only as fallback in case the |
| * application can not be launched through deployment artifacts, |
| * e.g., in IDEs with limited FX support. NetBeans ignores main(). |
| * @param args the command line arguments |
| */ |
| public static void main(String[] args) { |
| launch(args); |
| } |
| } |