1/*
2 * Copyright (c) 2009-2010 jMonkeyEngine
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 *   notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 *   notice, this list of conditions and the following disclaimer in the
14 *   documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17 *   may be used to endorse or promote products derived from this software
18 *   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 LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32package com.jme3.cinematic.events;
33
34import com.jme3.animation.LoopMode;
35import com.jme3.app.Application;
36import com.jme3.audio.AudioNode;
37import com.jme3.cinematic.Cinematic;
38import com.jme3.export.InputCapsule;
39import com.jme3.export.JmeExporter;
40import com.jme3.export.JmeImporter;
41import com.jme3.export.OutputCapsule;
42import java.io.IOException;
43
44/**
45 * A sound track to be played in a cinematic.
46 * @author Nehon
47 */
48public class SoundTrack extends AbstractCinematicEvent {
49
50    protected String path;
51    protected AudioNode audioNode;
52    protected boolean stream = false;
53
54    /**
55     * creates a sound track from the given resource path
56     * @param path the path to an audi file (ie : "Sounds/mySound.wav")
57     */
58    public SoundTrack(String path) {
59        this.path = path;
60    }
61
62    /**
63     * creates a sound track from the given resource path
64     * @param path the path to an audi file (ie : "Sounds/mySound.wav")
65     * @param stream true to make the audio data streamed
66     */
67    public SoundTrack(String path, boolean stream) {
68        this(path);
69        this.stream = stream;
70    }
71
72    public SoundTrack(String path, boolean stream, float initialDuration) {
73        super(initialDuration);
74        this.path = path;
75        this.stream = stream;
76    }
77
78    public SoundTrack(String path, boolean stream, LoopMode loopMode) {
79        super(loopMode);
80        this.path = path;
81        this.stream = stream;
82    }
83
84    public SoundTrack(String path, boolean stream, float initialDuration, LoopMode loopMode) {
85        super(initialDuration, loopMode);
86        this.path = path;
87        this.stream = stream;
88    }
89
90    public SoundTrack(String path, float initialDuration) {
91        super(initialDuration);
92        this.path = path;
93    }
94
95    public SoundTrack(String path, LoopMode loopMode) {
96        super(loopMode);
97        this.path = path;
98    }
99
100    public SoundTrack(String path, float initialDuration, LoopMode loopMode) {
101        super(initialDuration, loopMode);
102        this.path = path;
103    }
104
105    public SoundTrack() {
106    }
107
108    @Override
109    public void initEvent(Application app, Cinematic cinematic) {
110        super.initEvent(app, cinematic);
111        audioNode = new AudioNode(app.getAssetManager(), path, stream);
112        setLoopMode(loopMode);
113    }
114
115    @Override
116    public void setTime(float time) {
117        super.setTime(time);
118        //can occur on rewind
119        if (time < 0) {
120            stop();
121        }
122        audioNode.setTimeOffset(time);
123    }
124
125    @Override
126    public void onPlay() {
127        audioNode.play();
128    }
129
130    @Override
131    public void onStop() {
132        audioNode.stop();
133
134    }
135
136    @Override
137    public void onPause() {
138        audioNode.pause();
139    }
140
141    @Override
142    public void onUpdate(float tpf) {
143        if (audioNode.getStatus() == AudioNode.Status.Stopped) {
144            stop();
145        }
146    }
147
148    /**
149     *  Returns the underlying audion node of this sound track
150     * @return
151     */
152    public AudioNode getAudioNode() {
153        return audioNode;
154    }
155
156    @Override
157    public void setLoopMode(LoopMode loopMode) {
158        super.setLoopMode(loopMode);
159
160        if (loopMode != LoopMode.DontLoop) {
161            audioNode.setLooping(true);
162        } else {
163            audioNode.setLooping(false);
164        }
165    }
166
167    @Override
168    public void write(JmeExporter ex) throws IOException {
169        super.write(ex);
170        OutputCapsule oc = ex.getCapsule(this);
171        oc.write(path, "path", "");
172        oc.write(stream, "stream", false);
173    }
174
175    @Override
176    public void read(JmeImporter im) throws IOException {
177        super.read(im);
178        InputCapsule ic = im.getCapsule(this);
179        path = ic.readString("path", "");
180        stream = ic.readBoolean("stream", false);
181
182    }
183}
184