SoundUtils.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package me.hammerle.supersnuvi.util;
  2. import java.io.File;
  3. import javax.sound.sampled.AudioFormat;
  4. import javax.sound.sampled.AudioInputStream;
  5. import javax.sound.sampled.AudioSystem;
  6. import javax.sound.sampled.SourceDataLine;
  7. import me.hammerle.supersnuvi.Game;
  8. public class SoundUtils
  9. {
  10. public enum Sound
  11. {
  12. DASH("sounds/dash.wav", true),
  13. DODGE("sounds/dodge.wav", true),
  14. LONDONER_JUMP("sounds/londoner_jump.wav", true),
  15. MIRROR_BREAK("sounds/mirror_break.wav", true),
  16. MIRROR_CRACK("sounds/mirror_crack.wav", true),
  17. SWORD_PULL("sounds/sword_pull.wav", true),
  18. SWORD_SHEATH("sounds/sword_sheath.wav", true),
  19. SWORD_SLASH("sounds/sword_slash.wav", true),
  20. COLLECT("sounds/collect.wav", true),
  21. JUMP("sounds/jump.wav", true),
  22. JUMP_ON_BOUNCE_SHROOM("sounds/jump_on_bounce_shroom.wav", false),
  23. STONE_CRUMBLING("sounds/stone_crumbling.wav", true),
  24. WALK("sounds/walk.wav", false),
  25. WALK_WATER("sounds/walk_water.wav", false),
  26. MENU_MUSIC("sounds/menu_music.wav", false),
  27. SONG_1("sounds/song1.wav", false);
  28. private boolean isLooping = true;
  29. private volatile boolean isRunning = false;
  30. private SourceDataLine line = null;
  31. private AudioFormat format = null;
  32. private File f;
  33. private Thread thread = null;
  34. private byte[] cache = null;
  35. private boolean reset;
  36. Sound(String sound, boolean reset)
  37. {
  38. this.reset = reset;
  39. f = new File(sound);
  40. try
  41. {
  42. AudioFormat oldFormat = AudioSystem.getAudioFileFormat(f).getFormat();
  43. AudioFormat.Encoding[] encodings = AudioSystem.getTargetEncodings(oldFormat);
  44. if(encodings.length == 0)
  45. {
  46. return;
  47. }
  48. else
  49. {
  50. AudioFormat[] formats = AudioSystem.getTargetFormats(encodings[0], oldFormat);
  51. if(formats.length == 0)
  52. {
  53. return;
  54. }
  55. else
  56. {
  57. format = new AudioFormat(
  58. formats[0].getEncoding(),
  59. oldFormat.getSampleRate(),
  60. formats[0].getSampleSizeInBits(),
  61. formats[0].getChannels(),
  62. formats[0].getFrameSize(),
  63. oldFormat.getFrameRate(),
  64. formats[0].isBigEndian());
  65. line = AudioSystem.getSourceDataLine(format);
  66. }
  67. }
  68. thread = new Thread(() -> run());
  69. thread.start();
  70. }
  71. catch(Exception ex)
  72. {
  73. System.out.println("Sound '" + f + "' failed.");
  74. //ex.printStackTrace();
  75. }
  76. }
  77. private void run()
  78. {
  79. try
  80. {
  81. while(isLooping)
  82. {
  83. if(!isRunning)
  84. {
  85. synchronized(this)
  86. {
  87. wait();
  88. if(!isLooping)
  89. {
  90. return;
  91. }
  92. }
  93. }
  94. isRunning = false;
  95. line.open(format);
  96. line.start();
  97. if(cache != null)
  98. {
  99. line.write(cache, 0, cache.length);
  100. }
  101. else
  102. {
  103. AudioInputStream stream = AudioSystem.getAudioInputStream(format, AudioSystem.getAudioInputStream(f));
  104. byte[] buffer = new byte[1024 * 1024];
  105. int read = stream.read(buffer);
  106. if(read < buffer.length)
  107. {
  108. cache = new byte[read];
  109. System.arraycopy(buffer, 0, cache, 0, read);
  110. line.write(cache, 0, cache.length);
  111. }
  112. else
  113. {
  114. while(read > 0)
  115. {
  116. line.write(buffer, 0, read);
  117. read = stream.read(buffer);
  118. }
  119. }
  120. }
  121. line.drain();
  122. line.close();
  123. }
  124. }
  125. catch(Exception ex)
  126. {
  127. System.out.println("Sound '" + f + "' failed.");
  128. //ex.printStackTrace();
  129. }
  130. }
  131. public void play()
  132. {
  133. if(reset)
  134. {
  135. stop();
  136. }
  137. if(thread == null)
  138. {
  139. System.out.println("Playing sound '" + f + "' failed.");
  140. return;
  141. }
  142. isRunning = true;
  143. synchronized(this)
  144. {
  145. notify();
  146. }
  147. }
  148. public void stop()
  149. {
  150. if(thread == null)
  151. {
  152. System.out.println("Stopping sound '" + f + "' failed.");
  153. return;
  154. }
  155. isRunning = false;
  156. line.stop();
  157. }
  158. public void close()
  159. {
  160. if(thread == null)
  161. {
  162. System.out.println("Closing sound '" + f + "' failed.");
  163. return;
  164. }
  165. isLooping = false;
  166. synchronized(this)
  167. {
  168. notify();
  169. }
  170. line.close();
  171. }
  172. }
  173. public static void playSound(Sound sound)
  174. {
  175. if(Game.get().isSoundEnabled() && sound != null)
  176. {
  177. /*if(sound != Sound.MENU_MUSIC && sound != Sound.SONG_1 && sound != Sound.WALK)
  178. {
  179. System.out.println("PLAYING " + sound);
  180. }*/
  181. sound.play();
  182. }
  183. }
  184. public static void stopSound(Sound sound)
  185. {
  186. sound.stop();
  187. }
  188. public static void turnSoundOff()
  189. {
  190. for(Sound sound : Sound.values())
  191. {
  192. sound.stop();
  193. }
  194. }
  195. public static void closeSounds()
  196. {
  197. for(Sound sound : Sound.values())
  198. {
  199. sound.close();
  200. }
  201. }
  202. }