RLPark 1.0.0
Reinforcement Learning Framework in Java

CritterbotSound.java

Go to the documentation of this file.
00001 package rlpark.plugin.critterbot.internal;
00002 
00003 import java.io.IOException;
00004 
00005 import rlpark.plugin.critterbot.data.CritterbotLabels;
00006 import rlpark.plugin.rltoys.envio.observations.Legend;
00007 import rlpark.plugin.robot.internal.disco.datagroup.DropScalarGroup;
00008 import rlpark.plugin.robot.internal.disco.drops.Drop;
00009 import rlpark.plugin.robot.internal.disco.drops.DropArray;
00010 import rlpark.plugin.robot.internal.disco.drops.DropData;
00011 import rlpark.plugin.robot.internal.disco.io.DiscoPacket;
00012 import rlpark.plugin.robot.internal.disco.io.DiscoSocket;
00013 import rlpark.plugin.robot.internal.sync.Syncs;
00014 import rlpark.plugin.robot.observations.ObservationReceiver;
00015 import rlpark.plugin.robot.observations.ObservationVersatile;
00016 
00017 public class CritterbotSound implements ObservationReceiver {
00018   private final String hostname;
00019   private final int port;
00020   private DiscoSocket socket;
00021   public static final int NbMicrophoneBin = 9;
00022   private final Drop soundFFTDrop = new Drop("FftAudioDrop", new DropData[] {
00023       new DropArray(CritterbotLabels.MicrophoneFFT + "Left", NbMicrophoneBin),
00024       new DropArray(CritterbotLabels.MicrophoneFFT + "Right", NbMicrophoneBin) });
00025   private final DropScalarGroup soundData = new DropScalarGroup(soundFFTDrop);
00026 
00027   public CritterbotSound(String hostname, int port) {
00028     this.hostname = hostname;
00029     this.port = port;
00030   }
00031 
00032   @Override
00033   public void initialize() {
00034     try {
00035       socket = new DiscoSocket(hostname, port);
00036     } catch (IOException e) {
00037       e.printStackTrace();
00038     }
00039   }
00040 
00041   @Override
00042   public ObservationVersatile waitForData() {
00043     DiscoPacket packet = null;
00044     try {
00045       packet = socket.recv();
00046     } catch (IOException e) {
00047       e.printStackTrace();
00048     }
00049     return packet != null ? Syncs.createObservation(System.currentTimeMillis(), packet.byteBuffer(), soundData) : null;
00050   }
00051 
00052   @Override
00053   public boolean isClosed() {
00054     return socket == null || socket.isSocketClosed();
00055   }
00056 
00057   public Legend legend() {
00058     return soundData.legend();
00059   }
00060 
00061   @Override
00062   public int packetSize() {
00063     return soundFFTDrop.dataSize();
00064   }
00065 
00066   @Override
00067   public void close() {
00068     socket.close();
00069   }
00070 }
 All Classes Namespaces Files Functions Variables Enumerations
Zephyr
RLPark