import javax.sound.midi.*; /* botcafe by Thomas Feuerstein (art) http://feuerstein.myzel.net/ & Peter Chiochetti (code) http://arton.cunst.net/ Nov. 2006 */ /* Config */ // Applet Size int tw = 768; int aw = 500; int ah = 500; // Image Size int iw = 50; int ih = 50; // Zoom Factor int zf = 10; // Text Positions int p1 = 225; int p2 = 275; int p3 = 400; /* End Config */ // Globals BufferedReader reader; boolean mapped; boolean swapped; PImage map; PImage mab; int count; String isrc; String gsrc; color client; color black = 0xFF000000; color white = 0xFFFFFFFF; PFont font; String [] agents; // user agents array String [] dtimes; // access timestamps array String [] mnotes; // music notation array Sequencer seq; Sequence mseq; // midi sequence Track mtrk; // midi track int mcur; // current midi time private void playMidi() { if (!mapped) { status("Server Music not ready yet!"); return; } // XXX: other checks! try { if (seq.isRunning()) { seq.stop(); seq.close(); status("Server Music stopped..."); return; } } catch (Exception e) { } try { seq = MidiSystem.getSequencer(); seq.open(); } catch (MidiUnavailableException f) { System.out.println("No Midi"); System.out.println(f); } try { seq.setSequence(mseq); } catch (InvalidMidiDataException e) { System.out.println(e); } seq.start(); status("Server Music started..."); } private MidiEvent createEvent(int m, int c, int n, int v, int t) { ShortMessage mmsg = new ShortMessage(); try { mmsg.setMessage(m, c, n, v); } catch (InvalidMidiDataException e) { System.out.println(e); } return new MidiEvent(mmsg, t); } private void pushNote(int c, int n, int v, int d, int p) { mcur += p; mtrk.add(createEvent(ShortMessage.NOTE_ON, c, n, v, mcur)); mtrk.add(createEvent(ShortMessage.NOTE_OFF, c, n, 0, mcur + d * 250)); } private void prepareMusic() { mtrk.add(createEvent(ShortMessage.PROGRAM_CHANGE, 2, 0, 0, 0)); mtrk.add(createEvent(ShortMessage.PROGRAM_CHANGE, 3, 123, 0, 0)); mtrk.add(createEvent(ShortMessage.PROGRAM_CHANGE, 4, 127, 0, 0)); String[] evs; for (int i = 0; i < mnotes.length; i++) { String str = mnotes[i]; if (str == null) break; evs = str.split(" "); // chan, note, vel, dur, pause pushNote(Integer.parseInt(evs[0]), Integer.parseInt(evs[1]), Integer.parseInt(evs[2]), Integer.parseInt(evs[3]), Integer.parseInt(evs[4])); } } void setup () { size (tw, ah); background(white); background(loadImage("ui.png")); mapped = false; swapped = false; count = 0; textAlign(LEFT); noSmooth(); Graphics2D g2 = ((PGraphicsJava2D)g).g2; g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); font = loadFont("monospaced-14.vlw"); textFont(font, 14); // Midi setup mcur = 0; try { mseq = new Sequence(Sequence.PPQ, 250); } catch(InvalidMidiDataException e) { System.out.println("No Midi"); System.out.println(e); } mtrk = mseq.createTrack(); gsrc = param("gsrc"); if (gsrc == null) gsrc = "http://localhost/myzel/botcafe/data/geoip.php"; gsrc += "/"; isrc = param("isrc"); if (isrc == null) isrc = "http://localhost/myzel/botcafe/data/argb.php"; reader = createReader(isrc); String line = null; try { line = reader.readLine(); } catch (IOException e) { System.err.println("Exception caught: " + e.getMessage()); mapped = true; } if (line == null) { status("Failed to initialize log reader."); line = "FFFFFFFF"; mapped = true; } // Client try { client = color(unhex(line)); } catch (NumberFormatException e) { System.err.println("Exception caught: " + e.getMessage()); status("Data source corrupt."); mapped = true; } noStroke(); fill(white); rect(aw + 10, p1, 200, 20); stroke(black); fill(client); rect(aw + 20, p1, 20, 20); fill(black); text(int(red(client)) + "." + int(green(client)) + "." + int(blue(client)) + "." + int(alpha(client)), aw + 50, p1 + 20); //Log Line noStroke(); fill(white); rect(aw + 10, p2, 200, 20); stroke(black); rect(aw + 20, p2, 20, 20); fill(black); text("?.?.?.?", aw + 50, p2 + 20); mab = createImage(iw, ih, ARGB); map = createImage(iw, ih, ARGB); map.loadPixels(); agents = new String[aw*ah]; dtimes = new String[aw*ah]; mnotes = new String[aw*ah]; } // read lines from the log void getaline() { String line = null; // IP Address ARGB/RGBA try { line = reader.readLine(); } catch (IOException e) { System.err.println("Exception caught: " + e.getMessage()); mapped = true; return; } if (line == null) { status("Error reading log."); mapped = true; return; } // Save To Image map.pixels[count] = color(unhex(line)); map.updatePixels(); // Draw To Window int x = count % ih; int y = count / ih; noStroke(); fill(color(unhex(line))); rect(x * zf, y * zf, zf, zf); // Date/Time try { line = reader.readLine(); } catch (IOException e) { System.err.println("Exception caught: " + e.getMessage()); mapped = true; return; } if (line == null) { status("Error reading log."); mapped = true; return; } dtimes[count] = line; // User Agent try { line = reader.readLine(); } catch (IOException e) { System.err.println("Exception caught: " + e.getMessage()); mapped = true; return; } if (line == null) { status("Error reading log."); mapped = true; return; } agents[count] = line; // Notes/Midi try { line = reader.readLine(); } catch (IOException e) { System.err.println("Exception caught: " + e.getMessage()); mapped = true; return; } if (line == null) { status("Error reading log."); mapped = true; return; } mnotes[count] = line; // finish count++; int div = iw * ih / 100; if (count % div == 0) status(count / div + "%"); if (count == iw * ih) { prepareMusic(); mapped = true; status("Ready"); noLoop(); } } void draw () { if (!mapped) { getaline(); return; } } void mouseMoved() { if (mouseX >= aw) return; if (mouseY >= ah) return; int x = mouseX / zf; int y = mouseY / zf; int w = aw / zf; // Update Current Address color c = map.pixels[x + y * w]; noStroke(); fill(white); rect(aw + 10, p2, 200, 70); stroke(black); fill(c); rect(aw + 20, p2, 20, 20); fill(black); textFont(font, 14); text(int(red(c)) + "." + int(green(c)) + "." + int(blue(c)) + "." + int(alpha(c)), aw + 50, p2 + 20); // Date/Time if(dtimes[x + y * w] != null) text(dtimes[x + y * w], aw + 20, p2 + 40); // User Agent if(agents[x + y * w] != null) text(agents[x + y * w], aw + 20, p2 + 60); redraw(); } // show only selected address in black void swapDisplaySelect(color c) { int n = map.width * map.height; if (keyPressed == true && keyCode == SHIFT) { for (int i=0; i px1 && mouseX < px2 && mouseY > py1 && mouseY < py2) { playMidi(); return; } if (mouseX >= aw) return; if (mouseY >= ah) return; if (swapped) { swapDisplayOrig(); redraw(); return; } int x = mouseX / zf; int y = mouseY / zf; int w = aw / zf; color c = map.pixels[x + y * w]; if (c == 0) return; cursor(WAIT); // Update Patches Display swapDisplaySelect(c); redraw(); // Update Current Address String temp = new String(int(red(c)) + "." + int(green(c)) + "." + int(blue(c)) + "." + int(alpha(c))); String[] NFO = new String[5]; NFO = loadStrings(gsrc + temp); noStroke(); fill(white); rect(aw + 75, p3, 200, 90); fill(black); text(NFO[0], aw + 80, p3 + 16); text(NFO[1], aw + 80, p3 + 32); text(NFO[2], aw + 80, p3 + 48); text(NFO[3], aw + 80, p3 + 64); text(NFO[4], aw + 80, p3 + 80); redraw(); cursor(ARROW); }