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  */
32 
33 package com.jme3.audio;
34 
35 import com.jme3.asset.AssetKey;
36 import com.jme3.export.InputCapsule;
37 import com.jme3.export.JmeExporter;
38 import com.jme3.export.JmeImporter;
39 import com.jme3.export.OutputCapsule;
40 import java.io.IOException;
41 
42 /**
43  * <code>AudioKey</code> is extending AssetKey by holding stream flag.
44  *
45  * @author Kirill Vainer
46  */
47 public class AudioKey extends AssetKey<AudioData> {
48 
49     private boolean stream;
50     private boolean streamCache;
51 
52     /**
53      * Create a new AudioKey.
54      *
55      * @param name Name of the asset
56      * @param stream If true, the audio will be streamed from harddrive,
57      * otherwise it will be buffered entirely and then played.
58      * @param streamCache If stream is true, then this specifies if
59      * the stream cache is used. When enabled, the audio stream will
60      * be read entirely but not decoded, allowing features such as
61      * seeking, determining duration and looping.
62      */
AudioKey(String name, boolean stream, boolean streamCache)63     public AudioKey(String name, boolean stream, boolean streamCache){
64         this(name, stream);
65         this.streamCache = streamCache;
66     }
67 
68     /**
69      * Create a new AudioKey
70      *
71      * @param name Name of the asset
72      * @param stream If true, the audio will be streamed from harddrive,
73      * otherwise it will be buffered entirely and then played.
74      */
AudioKey(String name, boolean stream)75     public AudioKey(String name, boolean stream){
76         super(name);
77         this.stream = stream;
78     }
79 
AudioKey(String name)80     public AudioKey(String name){
81         super(name);
82         this.stream = false;
83     }
84 
AudioKey()85     public AudioKey(){
86     }
87 
88     @Override
toString()89     public String toString(){
90         return name + (stream ?
91                           (streamCache ?
92                             " (Stream/Cache)" :
93                             " (Stream)") :
94                          " (Buffer)");
95     }
96 
97     /**
98      * @return True if the loaded audio should be a {@link AudioStream} or
99      * false if it should be a {@link AudioBuffer}.
100      */
isStream()101     public boolean isStream() {
102         return stream;
103     }
104 
105     /**
106      * Specifies if the stream cache is used.
107      *
108      * When enabled, the audio stream will
109      * be read entirely but not decoded, allowing features such as
110      * seeking, looping and determining duration.
111      */
useStreamCache()112     public boolean useStreamCache(){
113         return streamCache;
114     }
115 
116     @Override
shouldCache()117     public boolean shouldCache(){
118         return !stream && !streamCache;
119     }
120 
121     @Override
write(JmeExporter ex)122     public void write(JmeExporter ex) throws IOException{
123         super.write(ex);
124         OutputCapsule oc = ex.getCapsule(this);
125         oc.write(stream, "do_stream", false);
126         oc.write(streamCache, "use_stream_cache", false);
127     }
128 
129     @Override
read(JmeImporter im)130     public void read(JmeImporter im) throws IOException{
131         super.read(im);
132         InputCapsule ic = im.getCapsule(this);
133         stream = ic.readBoolean("do_stream", false);
134         streamCache = ic.readBoolean("use_stream_cache", false);
135     }
136 
137 }
138