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.media.audioclip;
  33 
  34 import javafx.application.Application;
  35 import javafx.event.EventHandler;
  36 import javafx.scene.Group;
  37 import javafx.scene.Parent;
  38 import javafx.scene.Scene;
  39 import javafx.scene.effect.Light;
  40 import javafx.scene.effect.Lighting;
  41 import javafx.scene.input.MouseEvent;
  42 import javafx.scene.layout.StackPane;
  43 import javafx.scene.media.AudioClip;
  44 import javafx.scene.paint.Color;
  45 import javafx.scene.shape.Rectangle;
  46 import javafx.stage.Stage;
  47 
  48 /**
  49  * A sample that demonstrates the basics of AudioClips.
  50  *
  51  * @sampleName Audio Clip
  52  * @preview preview.png
  53  * @see javafx.scene.media.AudioClip
  54  * @related /Graphics 3d/Xylophone
  55  * @highlight
  56  * @conditionalFeatures WEB, MEDIA
  57  */
  58 public class AudioClipApp extends Application {
  59     public Parent createContent() {
  60         final double xStart = 12;
  61         final double xOffset = 30.0;
  62         final double barWidth = 22.0;
  63        
  64         Rectangle r1 = new Rectangle(0, 15, barWidth * 11.5, 10);
  65         r1.setFill(new Color(0.2, 0.12, 0.1, 1.0));
  66         Rectangle r2 = new Rectangle(0, -25, barWidth * 11.5, 10);
  67         r2.setFill(new Color(0.2, 0.12, 0.1, 1.0));
  68 
  69         final Group content = new Group(
  70                 r1,
  71                 r2,
  72                 createKey(Color.PURPLE, xStart + 0 * xOffset, barWidth, 100, "/ensemble/samples/shared-resources/Note1.wav"),
  73                 createKey(Color.BLUEVIOLET, xStart + 1 * xOffset, barWidth, 95, "/ensemble/samples/shared-resources/Note2.wav"),
  74                 createKey(Color.BLUE, xStart + 2 * xOffset, barWidth, 90, "/ensemble/samples/shared-resources/Note3.wav"),
  75                 createKey(Color.GREEN, xStart + 3 * xOffset, barWidth, 85, "/ensemble/samples/shared-resources/Note4.wav"),
  76                 createKey(Color.GREENYELLOW, xStart + 4 * xOffset, barWidth, 80, "/ensemble/samples/shared-resources/Note5.wav"),
  77                 createKey(Color.YELLOW, xStart + 5 * xOffset, barWidth, 75, "/ensemble/samples/shared-resources/Note6.wav"),
  78                 createKey(Color.ORANGE, xStart + 6 * xOffset, barWidth, 70, "/ensemble/samples/shared-resources/Note7.wav"),
  79                 createKey(Color.RED, xStart + 7 * xOffset, barWidth, 65, "/ensemble/samples/shared-resources/Note8.wav"));
  80 
  81         // A StackPane by default centers its children, here we extend it to 
  82         // scale the content to fill the StackPane first. 
  83         StackPane root = new StackPane() {
  84             @Override protected void layoutChildren() {
  85                 // find biggest scale that will fit while keeping proportions
  86                 double scale = Math.min(
  87                     (getWidth()-20) / content.getBoundsInLocal().getWidth(),
  88                     (getHeight()-20) / content.getBoundsInLocal().getHeight()
  89                 );
  90                 content.setScaleX(scale);
  91                 content.setScaleY(scale);
  92                 super.layoutChildren();
  93             }
  94         };
  95         root.getChildren().add(content);
  96         return root;
  97     }
  98 
  99     public static Rectangle createKey(Color color, double x, double width, double height, String sound) {
 100         // create a audio clip that this key will play
 101         final AudioClip barNote = new AudioClip(
 102                 AudioClipApp.class.getResource(sound).toExternalForm());
 103         // create the rectangle that draws the key                
 104         Rectangle rectangle = new Rectangle(x, -(height / 2), width, height);
 105         rectangle.setFill(color);
 106         Lighting lighting = new Lighting(new Light.Point(-20, -20, 100, Color.WHITE));
 107         lighting.setSurfaceScale(1);
 108         rectangle.setEffect(lighting);
 109         rectangle.setOnMousePressed((MouseEvent me) -> {
 110             barNote.play();
 111         });
 112         return rectangle;
 113     }
 114 
 115     @Override public void start(Stage primaryStage) throws Exception {
 116         primaryStage.setScene(new Scene(createContent()));
 117         primaryStage.show();
 118     }
 119     
 120     /** 
 121      * Java main for when running without JavaFX launcher 
 122      * @param args command line arguments
 123      */
 124     public static void main(String[] args) { launch(args); }
 125 }