154 lines
4.9 KiB
Java
154 lines
4.9 KiB
Java
/*
|
|
* Copyright 2019 Chris Cromer
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
*
|
|
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
*
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
*
|
|
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*
|
|
*/
|
|
|
|
package cl.cromer.game.sound;
|
|
|
|
import cl.cromer.game.Constantes;
|
|
|
|
import javax.sound.sampled.*;
|
|
import java.io.BufferedInputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.util.logging.Logger;
|
|
|
|
/**
|
|
* This class handles sound
|
|
*/
|
|
public class Sound implements Constantes {
|
|
/**
|
|
* The sound clip to play
|
|
*/
|
|
private Clip sound;
|
|
/**
|
|
* The path to the sound
|
|
*/
|
|
private String path;
|
|
/**
|
|
* The logger
|
|
*/
|
|
private Logger logger;
|
|
|
|
/**
|
|
* Load the sound
|
|
*
|
|
* @param path The path to the sound resource
|
|
* @throws SoundException Thrown if the sound file could not be loaded
|
|
*/
|
|
public Sound(String path) throws SoundException {
|
|
this.path = path;
|
|
logger = getLogger(this.getClass(), SOUND_LOG_LEVEL);
|
|
InputStream inputStream = this.getClass().getResourceAsStream(path);
|
|
if (inputStream == null) {
|
|
throw new SoundException("Could not load sound: " + path);
|
|
}
|
|
AudioInputStream audioInputStream = null;
|
|
try {
|
|
audioInputStream = AudioSystem.getAudioInputStream(new BufferedInputStream(inputStream));
|
|
}
|
|
catch (UnsupportedAudioFileException | IOException e) {
|
|
logger.warning(e.getMessage());
|
|
}
|
|
try {
|
|
/*DataLine.Info info = null;
|
|
if (audioInputStream != null) {
|
|
info = new DataLine.Info(Clip.class, audioInputStream.getFormat());
|
|
}
|
|
sound = (Clip) AudioSystem.getLine(info);*/
|
|
sound = AudioSystem.getClip();
|
|
}
|
|
catch (LineUnavailableException e) {
|
|
logger.warning(e.getMessage());
|
|
}
|
|
try {
|
|
if (audioInputStream != null) {
|
|
sound.open(audioInputStream);
|
|
}
|
|
}
|
|
catch (LineUnavailableException | IOException e) {
|
|
logger.warning(e.getMessage());
|
|
}
|
|
logger.info("Opened sound: " + path);
|
|
}
|
|
|
|
/**
|
|
* Play the sound
|
|
* @throws SoundException Thrown if the sound clip is null
|
|
*/
|
|
public void play() throws SoundException {
|
|
if (sound == null) {
|
|
throw new SoundException("Sound is null!");
|
|
}
|
|
|
|
sound.start();
|
|
logger.info("Play sound: " + path);
|
|
}
|
|
|
|
/**
|
|
* Stop the sound
|
|
* @throws SoundException Thrown if the sound clip is null
|
|
*/
|
|
public void stop() throws SoundException {
|
|
if (sound == null) {
|
|
throw new SoundException("Sound is null!");
|
|
}
|
|
|
|
sound.stop();
|
|
logger.info("Stop sound: " + path);
|
|
}
|
|
|
|
public void setLoops(int loops) throws SoundException {
|
|
if (sound == null) {
|
|
throw new SoundException("Sound is null!");
|
|
}
|
|
sound.loop(loops);
|
|
}
|
|
|
|
/**
|
|
* Set the volume of the sound
|
|
*
|
|
* @param volume Volume between 0f and 1f
|
|
* @throws SoundException Thrown if the sound clip is null or the volume is out of range
|
|
*/
|
|
public void setVolume(float volume) throws SoundException {
|
|
if (sound == null) {
|
|
throw new SoundException("Sound is null!");
|
|
}
|
|
|
|
if (volume < 0f || volume > 1f) {
|
|
throw new SoundException("Invalid sound range!");
|
|
}
|
|
|
|
if (sound.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
|
|
FloatControl gainControl = (FloatControl) sound.getControl(FloatControl.Type.MASTER_GAIN);
|
|
float dB = (float) (Math.log(volume) / Math.log(10.0) * 20.0);
|
|
logger.info("Set volume " + path + ": " + dB);
|
|
gainControl.setValue(dB);
|
|
}
|
|
else if (sound.isControlSupported(FloatControl.Type.VOLUME)) {
|
|
logger.info("Gain control not supported");
|
|
float newVolume = 0;
|
|
if (volume > 0f) {
|
|
newVolume = ((volume * 100) * 65536) / 100;
|
|
}
|
|
FloatControl volumeControl = (FloatControl) sound.getControl(FloatControl.Type.VOLUME);
|
|
logger.info("Set volume " + path + ": " + newVolume);
|
|
volumeControl.setValue(newVolume);
|
|
}
|
|
else {
|
|
logger.info("No control to modify volume");
|
|
}
|
|
}
|
|
}
|