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.area.audio;
  33 
  34 
  35 import javafx.application.Application;
  36 import javafx.scene.Parent;
  37 import javafx.scene.Scene;
  38 import javafx.scene.chart.AreaChart;
  39 import javafx.scene.chart.NumberAxis;
  40 import javafx.scene.chart.XYChart;
  41 import javafx.scene.media.AudioSpectrumListener;
  42 import javafx.scene.media.Media;
  43 import javafx.scene.media.MediaPlayer;
  44 import javafx.stage.Stage;
  45 
  46 
  47 /**
  48  * An area chart that shows audio spectrum of a music file being played.
  49  *
  50  * @sampleName Audio Area Chart
  51  * @preview preview.png
  52  * @see javafx.scene.chart.AreaChart
  53  * @see javafx.scene.chart.Chart
  54  * @see javafx.scene.chart.NumberAxis
  55  * @see javafx.scene.chart.XYChart
  56  * @see javafx.scene.media.AudioSpectrumListener
  57  * @see javafx.scene.media.Media
  58  * @see javafx.scene.media.MediaPlayer
  59  * @docUrl http://docs.oracle.com/javafx/2/charts/jfxpub-charts.htm Using JavaFX Charts Tutorial
  60  */
  61 public class AudioAreaChartApp extends Application {
  62 
  63     private XYChart.Data<Number, Number>[] series1Data;
  64     private AudioSpectrumListener audioSpectrumListener;
  65     private static final String AUDIO_URI = System.getProperty("demo.audio.url", 
  66             "http://download.oracle.com/otndocs/javafx/JavaRap_Audio.mp4");
  67     private MediaPlayer audioMediaPlayer;
  68     private static final boolean PLAY_AUDIO = Boolean.parseBoolean(
  69             System.getProperty("demo.play.audio", "true"));
  70 
  71     public AudioAreaChartApp() {
  72         audioSpectrumListener = (double timestamp, double duration, float[] magnitudes, float[] phases) -> {
  73             for (int i = 0; i < series1Data.length; i++) {
  74                 series1Data[i].setYValue(magnitudes[i] + 60);
  75             }
  76         };
  77     }
  78 
  79     public void play() {
  80         startAudio();
  81     }
  82 
  83     @Override
  84     public void stop() {
  85         stopAudio();
  86     }
  87 
  88     public Parent createContent() {
  89         final NumberAxis xAxis = new NumberAxis(0, 128, 8);
  90         final NumberAxis yAxis = new NumberAxis(0, 50, 10);
  91         final AreaChart<Number, Number> ac = new AreaChart<>(xAxis, yAxis);
  92         // setup chart
  93         ac.getStylesheets().add(AudioAreaChartApp.class
  94                 .getResource("AudioAreaChart.css").toExternalForm());
  95         ac.setLegendVisible(false);
  96         ac.setTitle("Live Audio Spectrum Data");
  97         ac.setAnimated(false);
  98         xAxis.setLabel("Frequency Bands");
  99         yAxis.setLabel("Magnitudes");
 100         yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis, null, "dB"));
 101         // add starting data
 102         XYChart.Series<Number, Number> series = new XYChart.Series<>();
 103         series.setName("Audio Spectrum");
 104         //noinspection unchecked
 105         series1Data = new XYChart.Data[(int) xAxis.getUpperBound()];
 106         for (int i = 0; i < series1Data.length; i++) {
 107             series1Data[i] = new XYChart.Data<Number, Number>(i, 50);
 108             series.getData().add(series1Data[i]);
 109         }
 110         ac.getData().add(series);
 111         return ac;
 112     }
 113 
 114     private void startAudio() {
 115         if (PLAY_AUDIO) {
 116             getAudioMediaPlayer().
 117                     setAudioSpectrumListener(audioSpectrumListener);
 118             getAudioMediaPlayer().play();
 119         }
 120     }
 121 
 122     private void stopAudio() {
 123         if (getAudioMediaPlayer().getAudioSpectrumListener() == audioSpectrumListener) {
 124             getAudioMediaPlayer().pause();
 125         }
 126     }
 127 
 128     private MediaPlayer getAudioMediaPlayer() {
 129         if (audioMediaPlayer == null) {
 130             Media audioMedia = new Media(AUDIO_URI);
 131             audioMediaPlayer = new MediaPlayer(audioMedia);
 132             audioMediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
 133         }
 134         return audioMediaPlayer;
 135     }
 136     
 137     @Override public void start(Stage primaryStage) throws Exception {
 138         primaryStage.setScene(new Scene(createContent()));
 139         primaryStage.show();
 140         play();
 141     }
 142 
 143     /**
 144      * Java main for when running without JavaFX launcher
 145      * @param args command line arguments
 146      */
 147     public static void main(String[] args) {
 148         launch(args);
 149     }
 150 }