diff --git a/java/mimis.dll b/java/mimis.dll index 790db42..55d8bdc 100644 Binary files a/java/mimis.dll and b/java/mimis.dll differ diff --git a/java/src/mimis/Main.java b/java/src/mimis/Main.java index 18044d2..21601ec 100644 --- a/java/src/mimis/Main.java +++ b/java/src/mimis/Main.java @@ -1,5 +1,6 @@ package mimis; +import mimis.application.PhotoViewerApplication; import mimis.application.cmd.windows.gomplayer.GomPlayerApplication; import mimis.application.cmd.windows.winamp.WinampApplication; import mimis.application.cmd.windows.wmp.WMPApplication; @@ -37,7 +38,8 @@ public class Main { new MPCApplication(), new VLCApplication(), new WinampApplication(), - new iPodApplication()}; + new iPodApplication(), + new PhotoViewerApplication()}; deviceArray = new Device[] { new LircDevice(), new WiimoteDevice(), diff --git a/java/src/mimis/Worker.java b/java/src/mimis/Worker.java index 0229b30..2a97931 100644 --- a/java/src/mimis/Worker.java +++ b/java/src/mimis/Worker.java @@ -101,6 +101,6 @@ public abstract class Worker implements Runnable { } } } - + protected abstract void work(); } diff --git a/java/src/mimis/application/PhotoViewerApplication.java b/java/src/mimis/application/PhotoViewerApplication.java new file mode 100644 index 0000000..d3dc254 --- /dev/null +++ b/java/src/mimis/application/PhotoViewerApplication.java @@ -0,0 +1,106 @@ +package mimis.application; + +import mimis.Worker; +import mimis.application.robot.RobotApplication; +import mimis.exception.worker.ActivateException; +import mimis.exception.worker.DeactivateException; +import mimis.value.Action; +import mimis.value.Key; + +public class PhotoViewerApplication extends RobotApplication { + protected final static String TITLE = "Photo Viewer"; + + protected static final int ZOOM_SLEEP = 100; + protected static final int DELETE_SLEEP = 2000; + + protected ZoomWorker zoomWorker; + protected boolean fullscreen; + + public PhotoViewerApplication() { + super(TITLE); + zoomWorker = new ZoomWorker(); + fullscreen = false; + } + + public void stop() { + super.stop(); + zoomWorker.stop(); + } + + public void begin(Action action) { + try { + switch (action) { + case VOLUME_UP: + zoomWorker.activate(1); + break; + case VOLUME_DOWN: + zoomWorker.activate(-1); + break; + } + } catch (ActivateException e) { + log.error(e); + } + } + + public void end(Action action) { + log.trace("PhotoViewerApplication end: " + action); + switch (action) { + case VOLUME_UP: + case VOLUME_DOWN: + try { + zoomWorker.deactivate(); + } catch (DeactivateException e) { + log.error(e); + } + break; + case FORWARD: + break; + case REWIND: + break; + case NEXT: + press(Key.RIGHT); + break; + case PREVIOUS: + press(Key.LEFT); + break; + case MUTE: + press(Key.CONTROL); + press(Key.NUMPAD0); + release(Key.CONTROL); + break; + case FULLSCREEN: + press(fullscreen ? Key.ESCAPE : Key.F11); + fullscreen = !fullscreen; + break; + case DISLIKE: + boolean restore = false; + if (fullscreen) { + end(Action.FULLSCREEN); + sleep(DELETE_SLEEP); + restore = true; + } + press(Key.F16); + press('Y'); + if (restore) { + sleep(DELETE_SLEEP); + end(Action.FULLSCREEN); + } + break; + } + } + + protected class ZoomWorker extends Worker { + protected int zoomDirection; + + public void activate(int zoomDirection) throws ActivateException { + super.activate(); + this.zoomDirection = zoomDirection; + } + + public void work() { + Key key = zoomDirection > 0 ? Key.ADD : Key.SUBTRACT; + press(key); + sleep(ZOOM_SLEEP); + } + } +} diff --git a/java/src/mimis/application/cmd/windows/gomplayer/GomPlayerApplication.java b/java/src/mimis/application/cmd/windows/gomplayer/GomPlayerApplication.java index b00ba00..a214a89 100644 --- a/java/src/mimis/application/cmd/windows/gomplayer/GomPlayerApplication.java +++ b/java/src/mimis/application/cmd/windows/gomplayer/GomPlayerApplication.java @@ -34,7 +34,7 @@ public class GomPlayerApplication extends WindowsApplication { log.trace("GomPlayerApplication begin: " + action); try { switch (action) { - case VOLUME_UP: + case VOLUME_UP: volumeWorker.activate(1); break; case VOLUME_DOWN: diff --git a/java/src/mimis/application/robot/RobotApplication.java b/java/src/mimis/application/robot/RobotApplication.java new file mode 100644 index 0000000..8119096 --- /dev/null +++ b/java/src/mimis/application/robot/RobotApplication.java @@ -0,0 +1,39 @@ +package mimis.application.robot; + +import java.awt.AWTException; +import java.awt.Robot; + +import mimis.Application; +import mimis.exception.worker.ActivateException; +import mimis.value.Key; + +public class RobotApplication extends Application { + protected Robot robot; + + public RobotApplication(String title) { + super(title); + } + + public void activate() throws ActivateException { + try { + robot = new Robot(); + robot.setAutoWaitForIdle(true); + } catch (AWTException e) { + log.error(e); + throw new ActivateException(); + } + super.activate(); + } + + public void press(Key key) { + robot.keyPress(key.getCode()); + } + + public void press(char key) { + robot.keyPress(key); + } + + public void release(Key key) { + robot.keyRelease(key.getCode()); + } +} diff --git a/java/src/mimis/event/EventListener.java b/java/src/mimis/event/EventListener.java index 5be1369..7b56a44 100644 --- a/java/src/mimis/event/EventListener.java +++ b/java/src/mimis/event/EventListener.java @@ -37,10 +37,11 @@ public abstract class EventListener extends Worker { } public void stop() { - super.stop(); synchronized (work) { work.notifyAll(); } + + super.stop(); } public abstract void event(Event event); diff --git a/java/src/mimis/util/BufferedSound.java b/java/src/mimis/util/BufferedSound.java index 3cbab1f..5ada054 100644 --- a/java/src/mimis/util/BufferedSound.java +++ b/java/src/mimis/util/BufferedSound.java @@ -11,8 +11,8 @@ public class BufferedSound { this.sampleSize = sampleSize; } - public byte[] getReport(int paramInt) { - return soundData[paramInt]; + public byte[] getReport(int i) { + return soundData[i]; } public int numReports() { diff --git a/java/src/mimis/util/Sound.java b/java/src/mimis/util/Sound.java index 7c477ab..86a0d92 100644 --- a/java/src/mimis/util/Sound.java +++ b/java/src/mimis/util/Sound.java @@ -8,6 +8,9 @@ import java.util.TimerTask; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.DataLine; +import javax.sound.sampled.SourceDataLine; +import javax.sound.sampled.UnsupportedAudioFileException; import wiiusej.Wiimote; import mimis.device.wiimote.WiimoteDevice; @@ -27,62 +30,121 @@ public class Sound { public static final int yamaha_difflookup[] = { 1, 3, 5, 7, 9, 11, 13, 15, - -1, -3, -5, -7, -9, -11, -13, -15}; + -1, -3, -5, -7, -9, -11, -13, 15}; public Object object = new Object(); public static void main(String[] args) { + /*File file = new File("sound.wav"); + try { + AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file); + AudioFormat au = audioInputStream.getFormat(); + System.out.println(au.getSampleRate());// Hz + System.out.println(au.getSampleSizeInBits());// bits + } catch (Exception e) { + e.printStackTrace(); + }*/ new Sound().start(); } public void start() { + File file = new File("1kSine16 (3130).wav"); + play(file); + System.exit(0);//if (true) return; + WiimoteService wiimoteService = new WiimoteService(); try { WiimoteDevice wiimoteDevice = new WiimoteDevice(); Wiimote wiimote = wiimoteService.getDevice(wiimoteDevice); - File file = new File("sound.wav"); - AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file); - BufferedSound bufferedSound = bufferSound(audioInputStream); + AudioFormat audioFormat = audioInputStream.getFormat(); + // BufferedSound bufferedSound = bufferSound(audioInputStream); + System.out.println(audioInputStream.getFormat().getSampleRate()); + System.out.println(audioInputStream.getFormat().getFrameRate()); + + //System.out.println(bufferedSound.getSampleRate()); + //byte rate = (byte) (48000 / bufferedSound.getSampleRate()); + //wiimote.setSpeakerRate((byte) rate, (byte) 0x00); + + wiimote.setSpeakerFormat(PCM); + //wiimote.setSpeakerRate((byte) 0, rate); + wiimote.setSpeakerRate((byte) 0x00, (byte) (48000 / audioFormat.getSampleRate())); // pcm + //wiimote.setSpeakerRate((byte) 0xd0, (byte) 0x07); // adpcm + wiimote.setSpeakerVolume(1); wiimote.activateSpeaker(); - wiimote.setSpeakerConfig(PCM, bufferedSound.getSampleRate(), 1); + /* File file = new File("volbeat_pcm_u8_32_1500.raw"); + FileInputStream fin = new FileInputStream(file); + byte[] block = new byte[20]; + while (fin.read(block) != -1) { + wiimote.streamSpeakerData(block); + }*/ - AudioFormat audioFormat = audioInputStream.getFormat(); - double sampleSizeInBytes = audioFormat.getSampleSizeInBits() / 8D; - double samplesPerBlock = BLOCK_SIZE / sampleSizeInBytes; - int step = (int) Math.round(1000 * samplesPerBlock / audioFormat.getSampleRate()); - - playBufferedSound(wiimote, bufferedSound, step); - - object.wait(); + //playBufferedSound(wiimote, bufferedSound, step); + playSound(wiimote, audioInputStream); + synchronized (object) { + object.wait(); + } wiimoteService.exit(); } catch (Exception e) { e.printStackTrace(); } } + public void playSound(final Wiimote wiimote, final AudioInputStream audioInputStream) { + Timer timer = new Timer(); + TimerTask timerTask = new TimerTask() { + public void run() { + try { + byte[] buffer = new byte[20]; + if (audioInputStream.read(buffer) != -1) { + buffer[0] = -2; + System.out.format("%2x\n", buffer[0]); + wiimote.streamSpeakerData(buffer); + } else { + cancel(); + synchronized (object) { + object.notifyAll(); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + }; + AudioFormat audioFormat = audioInputStream.getFormat(); + double sampleSizeInBytes = audioFormat.getSampleSizeInBits() / 8D; + double samplesPerBlock = BLOCK_SIZE / sampleSizeInBytes; + int step = (int) Math.round(1000 * samplesPerBlock / (float) audioFormat.getSampleRate()); + System.out.println("step: " + step); + timer.scheduleAtFixedRate(timerTask, 0, step); + } + public void playBufferedSound(final Wiimote wiimote, final BufferedSound bufferedSound, int step) { Timer timer = new Timer(); TimerTask timerTask = new TimerTask() { int i = 0; public void run() { wiimote.streamSpeakerData(bufferedSound.getReport(i)); + /*if (i % 10 == 0) { + wiimote.setSpeakerVolume(Math.random()); + wiimote.setSpeakerConfig(); + }*/ if (++i > bufferedSound.numReports()) { cancel(); + object.notifyAll(); } } }; timer.scheduleAtFixedRate(timerTask, 0, step); - object.notifyAll(); } public BufferedSound bufferSound(AudioInputStream audioInputStream) throws IOException { AudioFormat audioFormat = audioInputStream.getFormat(); int size = (int) (audioInputStream.getFrameLength() * audioFormat.getFrameSize()); - byte[][] sound = new byte[size / BLOCK_SIZE + 1][BLOCK_SIZE + 1]; + byte[][] sound = new byte[size / BLOCK_SIZE + 1][BLOCK_SIZE]; for (int i = 0; i < sound.length; ++i) { byte[] block = new byte[BLOCK_SIZE]; int j = 0; @@ -94,15 +156,59 @@ public class Sound { j += read; } while (j < BLOCK_SIZE); for (j = 0; j < BLOCK_SIZE; ++j) { - if ((i * 20 + j) > size) { + if ((i * BLOCK_SIZE + j) > size) { break; } - sound[i][j] = block[j]; - //sound[i][j] = 0x34; - sound[i][j] = (byte) (Math.random() * 0xff); + //sound[i][j] = block[j]; + sound[i][j] = (byte) ((j % 2 == 0) ? 0x33 : 0xcc); + //sound[i][j] = (byte) (Math.random() * 0xff); } } audioInputStream.close(); return new BufferedSound(sound, (int) audioFormat.getSampleRate(), audioFormat.getSampleSizeInBits()); } + + public void play(File file) { + + + try { + AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file); + AudioFormat audioFormat = audioInputStream.getFormat(); + System.out.println(audioFormat.getEncoding()); + System.out.println(audioFormat.getSampleRate()); + System.out.println(audioFormat.getFrameRate()); + AudioFormat newFormat = new AudioFormat( + AudioFormat.Encoding.PCM_SIGNED, + 3130, 8, 1, 1, 3130, false); + AudioInputStream newAudioInputStream = new AudioInputStream( + audioInputStream, + newFormat, + audioInputStream.getFrameLength() * audioFormat.getFrameSize()); + play(newAudioInputStream); + } catch (UnsupportedAudioFileException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void play(AudioInputStream audioInputStream) { + try { + AudioFormat audioFormat = audioInputStream.getFormat(); + DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat); + SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo); + sourceDataLine.open(audioFormat); + sourceDataLine.start(); + int i; + byte[] buffer = new byte[128]; + while ((i = audioInputStream.read(buffer, 0, buffer.length)) != -1) { + sourceDataLine.write(buffer, 0, i); + } + sourceDataLine.drain(); + sourceDataLine.close(); + } + catch(Exception e) { + e.printStackTrace(); + } + } } diff --git a/java/src/wiiusej/WiiUseApi.java b/java/src/wiiusej/WiiUseApi.java index 61f1e5c..303cb32 100644 --- a/java/src/wiiusej/WiiUseApi.java +++ b/java/src/wiiusej/WiiUseApi.java @@ -359,8 +359,8 @@ public class WiiUseApi { native void activateSpeaker(int id); native void deactivateSpeaker(int id); native void setSpeakerFormat(int id, byte format); - native void setSpeakerRate(int id, int rate); + native void setSpeakerRate(int id, byte rate, byte freq); native void setSpeakerVolume(int id, double volume); - native void setSpeakerConfig(int id, byte format, int rate, double volume); + native void setSpeakerConfig(int id); native void streamSpeakerData(int id, byte[] block); } diff --git a/java/src/wiiusej/WiiUseApiManager.java b/java/src/wiiusej/WiiUseApiManager.java index bbefe2e..62305bc 100644 --- a/java/src/wiiusej/WiiUseApiManager.java +++ b/java/src/wiiusej/WiiUseApiManager.java @@ -692,16 +692,16 @@ public class WiiUseApiManager extends Thread { wiiuse.setSpeakerFormat(id, format); } - public void setSpeakerRate(int id, int rate) { - wiiuse.setSpeakerRate(id, rate); + public void setSpeakerRate(int id, byte rate, byte freq) { + wiiuse.setSpeakerRate(id, rate, freq); } public void setSpeakerVolume(int id, double volume) { wiiuse.setSpeakerVolume(id, volume); } - public void setSpeakerConfig(int id, byte format, int rate, double volume) { - wiiuse.setSpeakerConfig(id, format, rate, volume); + public void setSpeakerConfig(int id) { + wiiuse.setSpeakerConfig(id); } public void streamSpeakerData(int id, byte[] block) { diff --git a/java/src/wiiusej/Wiimote.java b/java/src/wiiusej/Wiimote.java index 6999d18..8194f40 100644 --- a/java/src/wiiusej/Wiimote.java +++ b/java/src/wiiusej/Wiimote.java @@ -530,19 +530,19 @@ public class Wiimote implements WiiUseApiListener { } public void setSpeakerFormat(byte format) { - manager.setSpeakerFormat(format, format); + manager.setSpeakerFormat(id, format); } - public void setSpeakerRate(int rate) { - manager.setSpeakerRate(id, rate); + public void setSpeakerRate(byte rate, byte freq) { + manager.setSpeakerRate(id, rate, freq); } public void setSpeakerVolume(double volume) { manager.setSpeakerVolume(id, volume); } - public void setSpeakerConfig(byte format, int rate, double volume) { - manager.setSpeakerConfig(id, format, rate, volume); + public void setSpeakerConfig() { + manager.setSpeakerConfig(id); } public void streamSpeakerData(byte[] block) { diff --git a/java/wiiuse.dll b/java/wiiuse.dll index 0e46215..bc0bf2e 100644 Binary files a/java/wiiuse.dll and b/java/wiiuse.dll differ diff --git a/java/wiiusej.dll b/java/wiiusej.dll index 8aa37fd..d233ced 100644 Binary files a/java/wiiusej.dll and b/java/wiiusej.dll differ