diff options
author | Christopher Baines <cbaines8@gmail.com> | 2011-09-14 14:41:46 +0100 |
---|---|---|
committer | Christopher Baines <cbaines8@gmail.com> | 2011-09-14 14:41:46 +0100 |
commit | d3aa36dfa804686ebc60eeb32c59f53d681d5e0c (patch) | |
tree | 0318b70ff4f61dab095fa9f721fb9b118df816a8 /PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java | |
parent | ae8432ffe5cea8421b04068d0fe62d8e15175505 (diff) | |
download | punchingbag-d3aa36dfa804686ebc60eeb32c59f53d681d5e0c.tar punchingbag-d3aa36dfa804686ebc60eeb32c59f53d681d5e0c.tar.gz |
More serial improements, Giggle and SimpleKeyboard now work, kinda.
Diffstat (limited to 'PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java')
-rw-r--r-- | PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java | 42 |
1 files changed, 26 insertions, 16 deletions
diff --git a/PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java b/PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java index 89c7f62..6a69503 100644 --- a/PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java +++ b/PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java @@ -17,20 +17,20 @@ import uk.ac.open.punchingbag.PunchingBag; public class Giggle implements ButtonListener { PunchingBag bag = PunchingBag.getBag(); - - String soundDir = System.getProperty("user.dir") - + System.getProperty("file.separator"); - - File[] laughs = { new File(soundDir + "G4.wav"), - new File(soundDir + "C5.wav"), new File(soundDir + "D5.wav"), - new File(soundDir + "E5.wav"), new File(soundDir + "F5.wav"), - new File(soundDir + "G5.wav") }; + + 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(); @@ -38,27 +38,37 @@ public class Giggle implements ButtonListener { @Override public void buttonPressed(int x, int y) { - // TODO Auto-generated method stub - + // laugh((int) (Math.random()*6)); + } @Override public void contact(Contact c) { System.out.println(c); bag.circleExpand(c.x, c.y, 16); - if (c.force > 520) { - //int laugh = c.force + 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()); + 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(); |