Guest User

SoundLoader

a guest
Apr 3rd, 2018
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. package com.Ghreborn.client.sound;
  2.  
  3. import java.io.File;
  4. import java.net.MalformedURLException;
  5.  
  6. import javax.sound.sampled.AudioInputStream;
  7. import javax.sound.sampled.AudioSystem;
  8. import javax.sound.sampled.Clip;
  9. import javax.sound.sampled.FloatControl;
  10. import javax.sound.sampled.LineUnavailableException;
  11. import javax.sound.sampled.UnsupportedAudioFileException;
  12.  
  13. import com.Ghreborn.client.signlink;
  14.  
  15. /**
  16. * Custom Sound System
  17. * @author Invision
  18. * @version 1.00
  19. **/
  20. public class SoundLoader {
  21.  
  22. /*
  23. * Directory of your sound files
  24. * format is WAV
  25. */
  26. private static final String DIRECTORY = signlink.findcachedir()+"/sounds/";
  27.  
  28. /*
  29. * Current volume state
  30. * 36 chosen for default 50% volume state
  31. */
  32. public static float settingModifier = 36f;
  33.  
  34. /*
  35. * Current volume state
  36. */
  37. public static boolean isMuted;
  38.  
  39. /*
  40. * Clips
  41. */
  42. private static Clip[] clipIndex = null;
  43.  
  44. /*
  45. * Get number of files in directory
  46. */
  47. private static final int getDirectoryLength() {
  48. return new File(DIRECTORY).list().length;
  49. }
  50.  
  51. /**
  52. * Loads the sound clips into memory
  53. * during startup to prevent lag if loading
  54. * them during runtime.
  55. **/
  56. public static void preloadSounds() {
  57. clipIndex = new Clip[getDirectoryLength()];
  58. int counter = 0;
  59. for (int i = 0; i < clipIndex.length; i++) {
  60. try {
  61. File f = new File(DIRECTORY+""+i+".wav");
  62. AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(f);
  63. clipIndex[i] = AudioSystem.getClip();
  64. clipIndex[i].open(audioInputStream);
  65. counter++;
  66. } catch (MalformedURLException e) {
  67. System.out.println("Sound effect not found: "+i);
  68. e.printStackTrace();
  69. return;
  70. } catch (UnsupportedAudioFileException e) {
  71. System.out.println("Unsupported format for sound: "+i);
  72. return;
  73. } catch (LineUnavailableException e) {
  74. e.printStackTrace();
  75. return;
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. return;
  79. }
  80. }
  81. System.out.println("Succesfully loaded: "+counter+" custom sound clips.");
  82. }
  83.  
  84. /**
  85. * Plays a sound
  86. * @param soundID - The ID of the sound
  87. * @param loop - How many times to loop this sound
  88. * @param distanceFromSource - The distance from the source in tiles
  89. */
  90. public static void playSound(final int soundID, int loop, int distanceFromSource) {
  91.  
  92. try {
  93. if (!isMuted) {
  94. clipIndex[soundID].setFramePosition(0);
  95. applyVolumeSetting(clipIndex[soundID], getDistanceModifier(distanceFromSource)*settingModifier);
  96. clipIndex[soundID].start();
  97. /* shows how to close line when clip is finished playing
  98. clipIndex[soundID].addLineListener(new LineListener() {
  99. public void update(LineEvent myLineEvent) {
  100. if (myLineEvent.getType() == LineEvent.Type.STOP)
  101. clipIndex[soundID].close();
  102. }
  103. });
  104. */
  105. }
  106. } catch (Exception e) {
  107. System.out.println("Error please report: ");
  108. e.printStackTrace();
  109. }
  110. }
  111.  
  112. /**
  113. * Applies volume setting to the clip
  114. * @param line - the Clip to adjust volume setting for
  115. * @param volume - the volume percentage (0-100)
  116. * @return - the volume with applied setting
  117. */
  118. public static float applyVolumeSetting(Clip line, double volume) {
  119. System.out.println("Modifying volume to "+volume);
  120. if (volume > 100.0) volume = 100.0;
  121. if (volume >= 0.0) {
  122. FloatControl ctrl = null;
  123. try {
  124. ctrl = (FloatControl)(line.getControl(FloatControl.Type.MASTER_GAIN));
  125. } catch (IllegalArgumentException iax1) {
  126. try {
  127. ctrl = (FloatControl)(line.getControl(FloatControl.Type.VOLUME));
  128. } catch (IllegalArgumentException iax2) {
  129. System.out.println("Controls.setVolume() not supported.");
  130. return -1;
  131. }
  132. }
  133. float minimum = ctrl.getMinimum();
  134. float maximum = ctrl.getMaximum();
  135. float newValue = (float)(minimum + volume * (maximum - minimum) / 100.0F);
  136. //System.out.println("System min: " + minimum);
  137. //System.out.println("System max: " + maximum);
  138. if (newValue <= ctrl.getMinimum())
  139. newValue = ctrl.getMinimum();
  140. if (newValue >= ctrl.getMaximum())
  141. newValue = ctrl.getMaximum();
  142.  
  143. ctrl.setValue(newValue);
  144. //System.out.println("Setting modifier = " + volume);
  145. //System.out.println("New value = " + newValue);
  146. return newValue;
  147. }
  148. return -1;
  149. }
  150.  
  151. /**
  152. * Calculates tile distance modifier
  153. * @param tileDistance - distance in tiles from source
  154. * @return - the distance modifier
  155. */
  156. public static float getDistanceModifier(int tileDistance) {
  157. if (tileDistance <= 0) {
  158. tileDistance = 0;
  159. }
  160. if (tileDistance >= 10) {
  161. tileDistance = 10;
  162. }
  163. float distanceModifier = 0;
  164. if (tileDistance == 10)
  165. distanceModifier = 0.40f;
  166. if (tileDistance == 9)
  167. distanceModifier = 0.55f;
  168. if (tileDistance == 8)
  169. distanceModifier = 0.60f;
  170. if (tileDistance == 7)
  171. distanceModifier = 0.65f;
  172. if (tileDistance == 6)
  173. distanceModifier = 0.70f;
  174. if (tileDistance == 5)
  175. distanceModifier = 0.75f;
  176. if (tileDistance == 4)
  177. distanceModifier = 0.80f;
  178. if (tileDistance == 3)
  179. distanceModifier = 0.85f;
  180. if (tileDistance == 2)
  181. distanceModifier = 0.90f;
  182. if (tileDistance == 1)
  183. distanceModifier = 0.95f;
  184. if (tileDistance == 0)
  185. distanceModifier = 1.00f;
  186.  
  187. return distanceModifier;
  188. }
  189.  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment