| /* |
| * 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 chartaudiobar; |
| |
| import javafx.application.Application; |
| import javafx.scene.Group; |
| import javafx.scene.Scene; |
| import javafx.scene.chart.BarChart; |
| import javafx.scene.chart.CategoryAxis; |
| 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 audio bar chart with sound and animation. |
| * |
| * @see javafx.scene.chart.BarChart |
| * @see javafx.scene.chart.Chart |
| * @see javafx.scene.chart.CategoryAxis |
| * @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 ChartAudioBar extends Application { |
| |
| private XYChart.Data<String, Number>[] series1Data; |
| private AudioSpectrumListener audioSpectrumListener; |
| |
| private static final String AUDIO_URI = System.getProperty("demo.audio.url","http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv"); |
| 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 BarChart<String, Number> createChart() { |
| final CategoryAxis xAxis = new CategoryAxis(); |
| final NumberAxis yAxis = new NumberAxis(0,50,10); |
| final BarChart<String,Number> bc = new BarChart<String,Number>(xAxis,yAxis); |
| bc.setId("barAudioDemo"); |
| bc.setLegendVisible(false); |
| bc.setAnimated(false); |
| bc.setBarGap(0); |
| bc.setCategoryGap(1); |
| bc.setVerticalGridLinesVisible(false); |
| // setup chart |
| bc.setTitle("Live Audio Spectrum Data"); |
| xAxis.setLabel("Frequency Bands"); |
| yAxis.setLabel("Magnitudes"); |
| yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis,null,"dB")); |
| // add starting data |
| XYChart.Series<String,Number> series1 = new XYChart.Series<String,Number>(); |
| series1.setName("Data Series 1"); |
| //noinspection unchecked |
| series1Data = new XYChart.Data[128]; |
| String[] categories = new String[128]; |
| for (int i=0; i<series1Data.length; i++) { |
| categories[i] = Integer.toString(i+1); |
| series1Data[i] = new XYChart.Data<String,Number>(categories[i],50); |
| series1.getData().add(series1Data[i]); |
| } |
| bc.getData().add(series1); |
| return bc; |
| } |
| |
| 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); |
| } |
| } |