Advertisement
Guest User

Generic Zombie Shooter Sounds Class

a guest
Oct 2nd, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.09 KB | None | 0 0
  1. /**
  2.     This file is part of Generic Zombie Shooter.
  3.  
  4.     Generic Zombie Shooter is free software: you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation, either version 3 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     Generic Zombie Shooter is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with Generic Zombie Shooter.  If not, see <http://www.gnu.org/licenses/>.
  16.  **/
  17. package genericzombieshooter.misc;
  18.  
  19. import java.io.IOException;
  20. import java.net.URL;
  21. import javax.sound.sampled.AudioFormat;
  22. import javax.sound.sampled.AudioInputStream;
  23. import javax.sound.sampled.AudioSystem;
  24. import javax.sound.sampled.Clip;
  25. import javax.sound.sampled.DataLine;
  26. import javax.sound.sampled.FloatControl;
  27. import javax.sound.sampled.LineUnavailableException;
  28. import javax.sound.sampled.UnsupportedAudioFileException;
  29.  
  30. /**
  31.  * Contains all pre-loaded sounds.
  32.  * @author Darin Beaudreau
  33.  */
  34. public enum Sounds {
  35.     // Weapon-Related
  36.     POPGUN("shoot2.wav", false),
  37.     RTPS("shoot1.wav", false),
  38.     BOOMSTICK("shotgun1.wav", false),
  39.     FLAMETHROWER("flamethrower.wav", true),
  40.     THROW("throw2.wav", false),
  41.     EXPLOSION("explosion2.wav", false),
  42.     LANDMINE_ARMED("landmine_armed.wav", false),
  43.     TELEPORT("teleport.wav", false),
  44.    
  45.     // Zombie-Related
  46.     MOAN1("zombie_moan_01.wav", false),
  47.     MOAN2("zombie_moan_02.wav", false),
  48.     MOAN3("zombie_moan_03.wav", false),
  49.     MOAN4("zombie_moan_04.wav", false),
  50.     MOAN5("zombie_moan_05.wav", false),
  51.     MOAN6("zombie_moan_06.wav", false),
  52.     MOAN7("zombie_moan_07.wav", false),
  53.     MOAN8("zombie_moan_08.wav", false),
  54.     POISONCLOUD("poison_cloud.wav", false),
  55.    
  56.     // Game Sounds
  57.     POWERUP("powerup.wav", false),
  58.     PURCHASEWEAPON("purchase_weapon.wav", false),
  59.     BUYAMMO("buy_ammo2.wav", false),
  60.     PAUSE("pause.wav", false),
  61.     UNPAUSE("unpause.wav", false);
  62.    
  63.     private Clip clip;
  64.     private boolean looped;
  65.  
  66.     Sounds(String filename, boolean loop) {
  67.         openClip(filename, loop);
  68.     }
  69.  
  70.     private synchronized void openClip(String filename, boolean loop) {
  71.         try {
  72.             URL audioFile = Sounds.class.getResource("/resources/sounds/" + filename);
  73.  
  74.             AudioInputStream audio = AudioSystem.getAudioInputStream(audioFile);
  75.             AudioFormat format = audio.getFormat();
  76.             DataLine.Info info = new DataLine.Info(Clip.class, format);
  77.             clip = (Clip) AudioSystem.getLine(info);
  78.  
  79.             clip.open(audio);
  80.         } catch (UnsupportedAudioFileException uae) {
  81.             System.out.println(uae);
  82.         } catch (IOException ioe) {
  83.             System.out.println(ioe);
  84.         } catch (LineUnavailableException lue) {
  85.             System.out.println(lue);
  86.         }
  87.         looped = loop;
  88.     }
  89.  
  90.     public synchronized void play() {
  91.         play(1.0);
  92.     }
  93.    
  94.     public synchronized void play(final double gain) {
  95.         Runnable soundPlay = new Runnable() {
  96.             @Override
  97.             public void run() {
  98.                 Clip clipCopy = (Clip)clip;
  99.                 FloatControl gainControl = (FloatControl)clipCopy.getControl(FloatControl.Type.MASTER_GAIN);
  100.                 float dB = (float)(Math.log(gain) / Math.log(10.0) * 20.0);
  101.                 gainControl.setValue(dB);
  102.                 if(!looped) reset(clipCopy);
  103.                 clipCopy.loop((looped)?Clip.LOOP_CONTINUOUSLY:0);
  104.                
  105.             }
  106.         };
  107.         new Thread(soundPlay).start();
  108.     }
  109.    
  110.     public synchronized void reset() {
  111.         reset(clip);
  112.     }
  113.    
  114.     public synchronized void reset(Clip clipCopy) {
  115.         synchronized(clipCopy) { clipCopy.stop(); }
  116.         clipCopy.setFramePosition(0);
  117.     }
  118.  
  119.     public static void init() {
  120.         values();
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement