aboutsummaryrefslogtreecommitdiff
path: root/PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java
blob: 6a69503308b6573fb86f0ddf7e9da1f1b6d04516 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package uk.ac.open.punchingbag.examples;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

import uk.ac.open.punchingbag.ButtonListener;
import uk.ac.open.punchingbag.Contact;
import uk.ac.open.punchingbag.PunchingBag;

public class Giggle implements ButtonListener {

	PunchingBag bag = PunchingBag.getBag();

	Clip lastClip;
	long lastContact = 0;

	String soundDir = System.getProperty("user.dir") + System.getProperty("file.separator");

	File[] laughs = { new File(soundDir + "Laugh1.wav"), new File(soundDir + "Laugh2.wav"), new File(soundDir + "Laugh3.wav"),
			new File(soundDir + "Laugh4.wav"), new File(soundDir + "Laugh5.wav"), new File(soundDir + "Laugh6.wav"), new File(soundDir + "Laugh6.wav") };

	public static void main(String[] args) {
		new Giggle();

	}

	Giggle() {
		bag.addButtonListener(this);
		bag.connectToArduinos();
	}

	@Override
	public void buttonPressed(int x, int y) {
		// laugh((int) (Math.random()*6));

	}

	@Override
	public void contact(Contact c) {
		System.out.println(c);
		bag.circleExpand(c.x, c.y, 16);
		if ( System.currentTimeMillis() > (lastContact + 250)|| lastContact == 0) {
			lastContact = System.currentTimeMillis();
			if (c.force > 516) {
				laugh(0);
			} else if (c.force > 500) {
				laugh(5);
			} else if (c.force < 500) {
				laugh(3);
			}
		}
	}

	void laugh(int laugh) {
		try {
			AudioInputStream sound = AudioSystem.getAudioInputStream(laughs[laugh]);
			DataLine.Info dataLine = new DataLine.Info(Clip.class, sound.getFormat());
			if (lastClip != null) {
				lastClip.stop();
			}
			Clip clip = (Clip) AudioSystem.getLine(dataLine);
			clip.open(sound);
			clip.start();
			lastClip = clip;
		} catch (LineUnavailableException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedAudioFileException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}