Advertisement
Guest User

Processing to SuperCollider

a guest
Mar 4th, 2012
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.93 KB | None | 0 0
  1. import supercollider.*;
  2. import oscP5.*;
  3. import netP5.*;
  4. import hypermedia.net.*;
  5.  
  6.  
  7. UDP udp;
  8. Synth synth;
  9. Synth pink;
  10. Synth market;
  11.  
  12. void setup ()
  13. {
  14.     size(800, 200);
  15.     market = new Synth("market");
  16.     pink = new Synth("pink-noise");
  17.  
  18.     pink.set("vol", 0.2);
  19.     pink.create();
  20.     market.create();
  21.    
  22.     udp = new UDP( this, 11001 );
  23.     udp.listen( true );
  24. }
  25.  
  26. void draw ()
  27. {
  28.     background(0);
  29. }
  30.  
  31.  
  32. void receive( byte[] data, String ip, int port )
  33. {
  34.   data = subset(data, 0, data.length-2);
  35.   String message = new String( data );
  36.  
  37.   //println(message);
  38.   ParseLine(message);
  39. }
  40.  
  41.  
  42. public void ParseLine(String line) {
  43.      if (line.startsWith("Ask") || line.startsWith("Bid") || line.startsWith("Vol")) {
  44.  
  45.          String test = line.replace(",", "").replace(" ", "").replace("BTC", "").replace("$", "").replace("Â", "").replace("USD", "");
  46.          String[] splitString = test.split("#");
  47.  
  48.          HashMap<String, Float> keyVal = new HashMap<String, Float>();
  49.          //println(line);
  50.          for (String item : splitString) {
  51.                String[] itemSplit = item.split(":");
  52.                String key = itemSplit[0];
  53.                Float amt;
  54.                Float price;
  55.                if(key.equals("Bid") || key.equals("Ask")){
  56.                    price = Float.parseFloat(itemSplit[3].substring(0,itemSplit[3].length()-2));
  57.                    keyVal.put("Price", price);
  58.                }
  59.                if(key.equals("Vol")){
  60.                    amt = Float.parseFloat(itemSplit[1].substring(0, itemSplit[1].length()-5));
  61.                }else{
  62.  
  63.  
  64.                    amt = Float.parseFloat(itemSplit[1]);
  65.                }
  66.  
  67.                keyVal.put(key, amt);
  68.          }
  69.          
  70.          for (Map.Entry entry : keyVal.entrySet()) {
  71.              if (entry.getKey().equals("Ask")) {
  72.                  HandleAskChanged((Float)entry.getValue(),keyVal.get("Price"));
  73.                  continue;
  74.              }
  75.  
  76.              if (entry.getKey().equals("Bid")) {
  77.                  HandleBidChanged((Float)entry.getValue(),keyVal.get("Price"));
  78.                  continue;
  79.              }
  80.  
  81.              if (entry.getKey().equals("Last")) {
  82.                  HandleLastChanged((Float)entry.getValue());
  83.                  continue;
  84.              }
  85.  
  86.              if (entry.getKey().equals("Low")) {
  87.                  HandleLowChanged((Float)entry.getValue());
  88.                  continue;
  89.              }
  90.  
  91.              if (entry.getKey().equals("High")) {
  92.                  HandleHighChanged((Float)entry.getValue());
  93.                  continue;
  94.              }
  95.  
  96.              if (entry.getKey().equals("Avg")) {
  97.                  HandleAvgChanged((Float)entry.getValue());
  98.                  continue;
  99.              }
  100.  
  101.              if (entry.getKey().equals("Vol")) {
  102.                  HandleVolChanged((Float)entry.getValue());
  103.                  continue;
  104.              }
  105.  
  106.            }
  107.        }
  108.    }
  109.  
  110.    
  111.    public void HandleAskChanged(float vol, float price) {
  112.        println("HandleAskChanged"+vol+" "+price);
  113.        market.set("freq", price);
  114.    }
  115.  
  116.    public void HandleBidChanged(float vol, float price) {
  117.        //println("HandleBidChanged"+vol+" "+price);
  118.    }
  119.  
  120.    public void HandleLastChanged(float lastChanged) {
  121.        //println("HandleLastChanged:"+lastChanged);
  122.    }
  123.  
  124.    public void HandleVolChanged(float vol) {
  125.       //println("HandleVolChanged:"+Math.log(Math.log(vol)+1)/2);
  126.       //println("HandleVolChanged:"+vol);
  127.       float logvol = Float.parseFloat(Math.log(Math.log(vol)+1)+"");
  128.       pink.set("vol", logvol);
  129.    }
  130.  
  131.    public void HandleLowChanged(float low) {
  132.       //println("HandleLowChanged:"+low);
  133.    }
  134.  
  135.    public void HandleHighChanged(float high) {
  136.      //println("HandleHighChanged:"+high);
  137.    }
  138.  
  139.    public void HandleAvgChanged(float avg) {
  140.      //println("HandleAvgChanged:"+avg);
  141.    }
  142.  
  143.  
  144. void exit ()
  145. {
  146.     market.free();
  147.     pink.free();
  148.     super.exit();
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement