1 /*
   2  * Copyright (c) 2008, 2014, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 package ensemble.samples.charts.bar.audio;
  33 
  34 
  35 import javafx.application.Application;
  36 import javafx.scene.Parent;
  37 import javafx.scene.Scene;
  38 import javafx.scene.chart.BarChart;
  39 import javafx.scene.chart.CategoryAxis;
  40 import javafx.scene.chart.NumberAxis;
  41 import javafx.scene.chart.XYChart;
  42 import javafx.scene.media.AudioSpectrumListener;
  43 import javafx.scene.media.Media;
  44 import javafx.scene.media.MediaPlayer;
  45 import javafx.stage.Stage;
  46 
  47 
  48 /**
  49  * Bar chart that shows audio spectrum of a music file being played.
  50  *
  51  * @sampleName Audio Bar Chart
  52  * @preview preview.png
  53  * @see javafx.scene.chart.BarChart
  54  * @see javafx.scene.chart.Chart
  55  * @see javafx.scene.chart.CategoryAxis
  56  * @see javafx.scene.chart.NumberAxis
  57  * @see javafx.scene.chart.XYChart
  58  * @see javafx.scene.media.AudioSpectrumListener
  59  * @see javafx.scene.media.Media
  60  * @see javafx.scene.media.MediaPlayer
  61  * @conditionalFeatures WEB
  62  */
  63 public class AudioBarChartApp extends Application {
  64 
  65     private XYChart.Data<String, Number>[] series1Data;
  66     private AudioSpectrumListener audioSpectrumListener;
  67     private static final String AUDIO_URI = System.getProperty("demo.audio.url",
  68             "http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv");
  69     private MediaPlayer audioMediaPlayer;
  70     private static final boolean PLAY_AUDIO = Boolean.parseBoolean(
  71             System.getProperty("demo.play.audio", "true"));
  72 
  73     public AudioBarChartApp() {
  74         audioSpectrumListener = (double timestamp, double duration, float[] magnitudes, float[] phases) -> {
  75             for (int i = 0; i < series1Data.length; i++) {
  76                 series1Data[i].setYValue(magnitudes[i] + 60);
  77             }
  78         };
  79     }
  80 
  81     public void play() {
  82         this.startAudio();
  83     }
  84 
  85     @Override
  86     public void stop() {
  87         this.stopAudio();
  88     }
  89 
  90     public Parent createContent() {
  91         final CategoryAxis xAxis = new CategoryAxis();
  92         final NumberAxis yAxis = new NumberAxis(0, 50, 10);
  93         final BarChart<String, Number> bc = new BarChart<>(xAxis, yAxis);
  94         bc.getStylesheets().add(AudioBarChartApp.class.getResource("AudioBarChart.css").toExternalForm());
  95         bc.setLegendVisible(false);
  96         bc.setAnimated(false);
  97         bc.setBarGap(0);
  98         bc.setCategoryGap(1);
  99         bc.setVerticalGridLinesVisible(false);
 100         // setup chart
 101         bc.setTitle("Live Audio Spectrum Data");
 102         xAxis.setLabel("Frequency Bands");
 103         yAxis.setLabel("Magnitudes");
 104         yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis, null, "dB"));
 105         // add starting data
 106         XYChart.Series<String, Number> series1 = new XYChart.Series<>();
 107         series1.setName("Data Series 1");
 108         //noinspection unchecked
 109         series1Data = new XYChart.Data[128];
 110         String[] categories = new String[128];
 111         for (int i = 0; i < series1Data.length; i++) {
 112             categories[i] = Integer.toString(i + 1);
 113             series1Data[i] = new XYChart.Data<String, Number>(categories[i], 50);
 114             series1.getData().add(series1Data[i]);
 115         }
 116         bc.getData().add(series1);
 117         return bc;
 118     }
 119 
 120     private void startAudio() {
 121         if (PLAY_AUDIO) {
 122             getAudioMediaPlayer()
 123                     .setAudioSpectrumListener(audioSpectrumListener);
 124             getAudioMediaPlayer().play();
 125         }
 126     }
 127 
 128     private void stopAudio() {
 129         if (getAudioMediaPlayer().getAudioSpectrumListener() == audioSpectrumListener) {
 130             getAudioMediaPlayer().pause();
 131         }
 132     }
 133 
 134     private MediaPlayer getAudioMediaPlayer() {
 135         if (audioMediaPlayer == null) {
 136             Media audioMedia = new Media(AUDIO_URI);
 137             audioMediaPlayer = new MediaPlayer(audioMedia);
 138             audioMediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
 139         }
 140         return audioMediaPlayer;
 141     }
 142 
 143     @Override
 144     public void start(Stage primaryStage) throws Exception {
 145         primaryStage.setScene(new Scene(createContent()));
 146         primaryStage.show();
 147         play();
 148     }
 149 
 150     /**
 151      * Java main for when running without JavaFX launcher
 152      * @param args command line arguments
 153      */
 154     public static void main(String[] args) {
 155         launch(args);
 156     }
 157 }