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