SoundPlayer.java
package prey;
 
import java.util.Random;
 
import lejos.nxt.Sound;
 
public class SoundPlayer {
    private static int volume = 80;
    private static Random rand = new Random();
 
    private static void playSound(Behavior behavior, int frequency, int duration, int timeout, int rangeMin, int rangeMax) throws InterruptBehaviorException {
        if (frequency > 0) {
            Sound.playTone(frequency, duration, volume);
        } else {
            frequency = rangeMin + rand.nextInt(rangeMax-rangeMin);
            Sound.playTone(frequency, duration, volume);
        }    
        behavior.delay(timeout);
    }
 
    private static void playSound(Behavior behavior, int frequency, int duration, int timeout) throws InterruptBehaviorException {
        playSound(behavior, frequency, duration, timeout, 0, 0);
    }
 
    private static void playSound(Behavior behavior, int rangeMin, int rangeMax, int duration, int timeout) throws InterruptBehaviorException {
        playSound(behavior, 0, duration, timeout, rangeMin, rangeMax);
    }
 
    private static void playSound(Behavior behavior, int rangeMin, int rangeMax) throws InterruptBehaviorException {
        playSound(behavior, 0, 5, 0, rangeMin, rangeMax);
    }
 
    public static void playRunAwaySound(Behavior behavior, int d) throws InterruptBehaviorException {
        int step = 20;
        for (int i = 0; i < d; i += step) {
            playSound(behavior, 1024+256, 1024+512, 10, step);
        }
    }
 
    public static void playEatFoodSound(Behavior behavior, int d) throws InterruptBehaviorException {
        int step = 50;
        for (int i = 0; i < d; i += step) {
            int soundFrequency = (100-InternalState.getHunger())*4 + 64;
            playSound(behavior, soundFrequency, soundFrequency + 128, 20, step);
        }
    }
 
    public static void playAvoidBorderSound(Behavior behavior) {
        try {
            playSound(behavior, 512, 576);
        } catch (InterruptBehaviorException e) {}
    }
 
    public static void playDieSound() {
        try {
            int d = rand.nextInt(3)*50 + 150; // duration.
            Sound.playTone(250, d, volume);
            Thread.sleep(d);
            Sound.playTone(200, d, volume);
            Thread.sleep(d);
            Sound.playTone(150, d, volume);
            Thread.sleep(d);
            Sound.playTone(100, d, volume);
            Thread.sleep(d);
        } catch (InterruptedException e) {}
    }
 
    public static void playWanderSound(Behavior behavior, int d) throws InterruptBehaviorException {
        int step = 200;
        for (int i = 0; i < d; i += step) {
            playSound(behavior, 256, 512, 50, step);
        }
    }
 
    public static void playBackBumperSound(Behavior behavior, int d) throws InterruptBehaviorException {
        int step = 200;
        for (int i = 0; i < d; i += step) {
            playSound(behavior, 512+256, 1024, 50, step);
        }
    }
 
    public static void playAvoidObstacle(Behavior behavior) throws InterruptBehaviorException {
        playSound(behavior, 45, 500, 0);
    }
 
    //    private static void delay(int time) throws InterruptBehaviorException {
    //        try {
    //            int step = 5;
    //            for (int i = 0; i < time; i += step) {
    //                Mover.interrupted();
    //                Thread.sleep(step);
    //            }
    //        } catch (InterruptedException e) {
    //        }
    //    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License