AudioKey.java 5.6 KB

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