\ru\ifmo\alarm\controlled\Sounds.java
|
package ru.ifmo.alarm.controlled;
import java.io.IOException;
import java.net.MalformedURLException;
import javax.sound.sampled.*;
import com.evelopers.unimod.runtime.ControlledObject;
import com.evelopers.unimod.runtime.context.StateMachineContext;
public class Sounds implements ControlledObject {
/*
* This are ID of different tunes
*/
private static final int DING = 0;
private static final int NOTIFY = 1;
private static final int START = 2;
private static final int CHIMES = 3;
/**
* This is path for sounds folder
*/
private static final String SOUND_PATH = "/ru/ifmo/alarm/sounds/";
/*
* This are file names of tunes
*/
private static final String dingFileName = "ding.wav";
private static final String notifyFileName = "notify.wav";
private static final String startFileName = "easilyhit.wav";
private static final String chimesFileName = "chimes.wav";
/*
* This are clip for playing sounds
*/
protected Clip clipDing;
protected Clip clipNotify;
protected Clip clipStart;
protected Clip clipChimes;
/**
* Current clip
*/
protected Clip currentClip;
/*
* This flag
*/
protected boolean isEnabled = true;
/**
* Class constructor. Loads all music clips.
*/
public Sounds(){
try {
AudioInputStream stream;
DataLine.Info info;
// Ding
stream = getAudioStream(dingFileName);
info = new DataLine.Info(Clip.class, stream.getFormat(),
((int)stream.getFrameLength() * stream.getFormat().getFrameSize()));
clipDing = (Clip) AudioSystem.getLine(info);
clipDing.open(stream);
// Notify
stream = getAudioStream(notifyFileName);
info = new DataLine.Info(Clip.class, stream.getFormat(),
((int)stream.getFrameLength() * stream.getFormat().getFrameSize()));
clipNotify = (Clip) AudioSystem.getLine(info);
clipNotify.open(stream);
// Start
stream = getAudioStream(startFileName);
info = new DataLine.Info(Clip.class, stream.getFormat(),
((int)stream.getFrameLength() * stream.getFormat().getFrameSize()));
clipStart = (Clip) AudioSystem.getLine(info);
clipStart.open(stream);
// Chimes
stream = getAudioStream(chimesFileName);
info = new DataLine.Info(Clip.class, stream.getFormat(),
((int)stream.getFrameLength() * stream.getFormat().getFrameSize()));
clipChimes = (Clip) AudioSystem.getLine(info);
clipChimes.open(stream);
} catch (IOException e)
{
System.out.println(e.getMessage());
} catch (LineUnavailableException e)
{
System.out.println(e.getMessage());
}
}
/**
* Converts encodings to PCM_SIGNED.
* @param fileName sound file name
*/
private AudioInputStream getAudioStream(String fileName){
try
{
AudioInputStream stream = AudioSystem.getAudioInputStream(
Sounds.class.getResource(SOUND_PATH + fileName));
AudioFormat format = stream.getFormat();
if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED){
format = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
format.getSampleSizeInBits()*2,
format.getChannels(),
format.getFrameSize()*2,
format.getFrameRate(),
true);
stream = AudioSystem.getAudioInputStream(format, stream);
}
return stream;
} catch (UnsupportedAudioFileException e)
{
System.out.println(e.getMessage());
} catch (MalformedURLException e)
{
System.out.println(e.getMessage());
} catch (IOException e)
{
System.out.println(e.getMessage());
}
return null;
}
/**
* Play sound
* @milliseconds - how long play sound
* @clipID - which clip to play
*/
private void play(long milliseconds, int clipID){
stop();
switch(clipID)
{
case DING:
currentClip = clipDing;
break;
case NOTIFY:
currentClip = clipNotify;
break;
case START:
currentClip = clipStart;
break;
case CHIMES:
currentClip = clipChimes;
break;
default:
return;
}
int times = (int)( 1000 * milliseconds / currentClip.getMicrosecondLength());
currentClip.setMicrosecondPosition(0);
currentClip.loop(times);
}
/**
* Stops all clips
*/
void stop()
{
if (currentClip != null && currentClip.isRunning()) {
currentClip.stop();
}
}
/**
* @unimod.action.descr alarm was turned on
*/
public void z1(StateMachineContext context) {
long ms = 500;
play(ms, NOTIFY);
}
/**
* @unimod.action.descr alarm was turned off
*/
public void z2(StateMachineContext context) {
long ms = 1500;
stop();
play(ms, DING);
}
/**
* @unimod.action.descr car was easily hit
*/
public void z3(StateMachineContext context) {
long ms = 1500;
play(ms, START);
}
/**
* @unimod.action.descr full alarm mode
*/
public void z4(StateMachineContext context) {
int ms = 20000;
play(ms, CHIMES);
}
/**
* @unimod.action.descr switch off sounds
*/
public void z5(StateMachineContext context) {
stop();
}
/**
* @unimod.action.descr set enabled
*/
public void z6(StateMachineContext context) {
isEnabled = true;
}
/**
* @unimod.action.descr set disenabled
*/
public void z7(StateMachineContext context) {
isEnabled = false;
}
/**
* @unimod.action.descr is enabled
*/
public boolean x1(StateMachineContext context) {
return isEnabled;
}
}