Diverse aanpassingen.

This commit is contained in:
2011-09-18 11:08:49 +00:00
parent f9bafb6fb9
commit bafe96b16b
14 changed files with 290 additions and 36 deletions

Binary file not shown.

View File

@@ -1,5 +1,6 @@
package mimis; package mimis;
import mimis.application.PhotoViewerApplication;
import mimis.application.cmd.windows.gomplayer.GomPlayerApplication; import mimis.application.cmd.windows.gomplayer.GomPlayerApplication;
import mimis.application.cmd.windows.winamp.WinampApplication; import mimis.application.cmd.windows.winamp.WinampApplication;
import mimis.application.cmd.windows.wmp.WMPApplication; import mimis.application.cmd.windows.wmp.WMPApplication;
@@ -37,7 +38,8 @@ public class Main {
new MPCApplication(), new MPCApplication(),
new VLCApplication(), new VLCApplication(),
new WinampApplication(), new WinampApplication(),
new iPodApplication()}; new iPodApplication(),
new PhotoViewerApplication()};
deviceArray = new Device[] { deviceArray = new Device[] {
new LircDevice(), new LircDevice(),
new WiimoteDevice(), new WiimoteDevice(),

View File

@@ -101,6 +101,6 @@ public abstract class Worker implements Runnable {
} }
} }
} }
protected abstract void work(); protected abstract void work();
} }

View File

@@ -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);
}
}
}

View File

@@ -34,7 +34,7 @@ public class GomPlayerApplication extends WindowsApplication {
log.trace("GomPlayerApplication begin: " + action); log.trace("GomPlayerApplication begin: " + action);
try { try {
switch (action) { switch (action) {
case VOLUME_UP: case VOLUME_UP:
volumeWorker.activate(1); volumeWorker.activate(1);
break; break;
case VOLUME_DOWN: case VOLUME_DOWN:

View File

@@ -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());
}
}

View File

@@ -37,10 +37,11 @@ public abstract class EventListener extends Worker {
} }
public void stop() { public void stop() {
super.stop();
synchronized (work) { synchronized (work) {
work.notifyAll(); work.notifyAll();
} }
super.stop();
} }
public abstract void event(Event event); public abstract void event(Event event);

View File

@@ -11,8 +11,8 @@ public class BufferedSound {
this.sampleSize = sampleSize; this.sampleSize = sampleSize;
} }
public byte[] getReport(int paramInt) { public byte[] getReport(int i) {
return soundData[paramInt]; return soundData[i];
} }
public int numReports() { public int numReports() {

View File

@@ -8,6 +8,9 @@ import java.util.TimerTask;
import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem; import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import wiiusej.Wiimote; import wiiusej.Wiimote;
import mimis.device.wiimote.WiimoteDevice; import mimis.device.wiimote.WiimoteDevice;
@@ -27,62 +30,121 @@ public class Sound {
public static final int yamaha_difflookup[] = { 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}; -1, -3, -5, -7, -9, -11, -13, 15};
public Object object = new Object(); public Object object = new Object();
public static void main(String[] args) { 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(); new Sound().start();
} }
public void start() { public void start() {
File file = new File("1kSine16 (3130).wav");
play(file);
System.exit(0);//if (true) return;
WiimoteService wiimoteService = new WiimoteService(); WiimoteService wiimoteService = new WiimoteService();
try { try {
WiimoteDevice wiimoteDevice = new WiimoteDevice(); WiimoteDevice wiimoteDevice = new WiimoteDevice();
Wiimote wiimote = wiimoteService.getDevice(wiimoteDevice); Wiimote wiimote = wiimoteService.getDevice(wiimoteDevice);
File file = new File("sound.wav");
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file); 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.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(); //playBufferedSound(wiimote, bufferedSound, step);
double sampleSizeInBytes = audioFormat.getSampleSizeInBits() / 8D; playSound(wiimote, audioInputStream);
double samplesPerBlock = BLOCK_SIZE / sampleSizeInBytes; synchronized (object) {
int step = (int) Math.round(1000 * samplesPerBlock / audioFormat.getSampleRate()); object.wait();
}
playBufferedSound(wiimote, bufferedSound, step);
object.wait();
wiimoteService.exit(); wiimoteService.exit();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); 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) { public void playBufferedSound(final Wiimote wiimote, final BufferedSound bufferedSound, int step) {
Timer timer = new Timer(); Timer timer = new Timer();
TimerTask timerTask = new TimerTask() { TimerTask timerTask = new TimerTask() {
int i = 0; int i = 0;
public void run() { public void run() {
wiimote.streamSpeakerData(bufferedSound.getReport(i)); wiimote.streamSpeakerData(bufferedSound.getReport(i));
/*if (i % 10 == 0) {
wiimote.setSpeakerVolume(Math.random());
wiimote.setSpeakerConfig();
}*/
if (++i > bufferedSound.numReports()) { if (++i > bufferedSound.numReports()) {
cancel(); cancel();
object.notifyAll();
} }
} }
}; };
timer.scheduleAtFixedRate(timerTask, 0, step); timer.scheduleAtFixedRate(timerTask, 0, step);
object.notifyAll();
} }
public BufferedSound bufferSound(AudioInputStream audioInputStream) throws IOException { public BufferedSound bufferSound(AudioInputStream audioInputStream) throws IOException {
AudioFormat audioFormat = audioInputStream.getFormat(); AudioFormat audioFormat = audioInputStream.getFormat();
int size = (int) (audioInputStream.getFrameLength() * audioFormat.getFrameSize()); 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) { for (int i = 0; i < sound.length; ++i) {
byte[] block = new byte[BLOCK_SIZE]; byte[] block = new byte[BLOCK_SIZE];
int j = 0; int j = 0;
@@ -94,15 +156,59 @@ public class Sound {
j += read; j += read;
} while (j < BLOCK_SIZE); } while (j < BLOCK_SIZE);
for (j = 0; j < BLOCK_SIZE; ++j) { for (j = 0; j < BLOCK_SIZE; ++j) {
if ((i * 20 + j) > size) { if ((i * BLOCK_SIZE + j) > size) {
break; break;
} }
sound[i][j] = block[j]; //sound[i][j] = block[j];
//sound[i][j] = 0x34; sound[i][j] = (byte) ((j % 2 == 0) ? 0x33 : 0xcc);
sound[i][j] = (byte) (Math.random() * 0xff); //sound[i][j] = (byte) (Math.random() * 0xff);
} }
} }
audioInputStream.close(); audioInputStream.close();
return new BufferedSound(sound, (int) audioFormat.getSampleRate(), audioFormat.getSampleSizeInBits()); 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();
}
}
} }

View File

@@ -359,8 +359,8 @@ public class WiiUseApi {
native void activateSpeaker(int id); native void activateSpeaker(int id);
native void deactivateSpeaker(int id); native void deactivateSpeaker(int id);
native void setSpeakerFormat(int id, byte format); 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 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); native void streamSpeakerData(int id, byte[] block);
} }

View File

@@ -692,16 +692,16 @@ public class WiiUseApiManager extends Thread {
wiiuse.setSpeakerFormat(id, format); wiiuse.setSpeakerFormat(id, format);
} }
public void setSpeakerRate(int id, int rate) { public void setSpeakerRate(int id, byte rate, byte freq) {
wiiuse.setSpeakerRate(id, rate); wiiuse.setSpeakerRate(id, rate, freq);
} }
public void setSpeakerVolume(int id, double volume) { public void setSpeakerVolume(int id, double volume) {
wiiuse.setSpeakerVolume(id, volume); wiiuse.setSpeakerVolume(id, volume);
} }
public void setSpeakerConfig(int id, byte format, int rate, double volume) { public void setSpeakerConfig(int id) {
wiiuse.setSpeakerConfig(id, format, rate, volume); wiiuse.setSpeakerConfig(id);
} }
public void streamSpeakerData(int id, byte[] block) { public void streamSpeakerData(int id, byte[] block) {

View File

@@ -530,19 +530,19 @@ public class Wiimote implements WiiUseApiListener {
} }
public void setSpeakerFormat(byte format) { public void setSpeakerFormat(byte format) {
manager.setSpeakerFormat(format, format); manager.setSpeakerFormat(id, format);
} }
public void setSpeakerRate(int rate) { public void setSpeakerRate(byte rate, byte freq) {
manager.setSpeakerRate(id, rate); manager.setSpeakerRate(id, rate, freq);
} }
public void setSpeakerVolume(double volume) { public void setSpeakerVolume(double volume) {
manager.setSpeakerVolume(id, volume); manager.setSpeakerVolume(id, volume);
} }
public void setSpeakerConfig(byte format, int rate, double volume) { public void setSpeakerConfig() {
manager.setSpeakerConfig(id, format, rate, volume); manager.setSpeakerConfig(id);
} }
public void streamSpeakerData(byte[] block) { public void streamSpeakerData(byte[] block) {

Binary file not shown.

Binary file not shown.