import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.UIManager; import javax.swing.plaf.metal.MetalLookAndFeel; import javax.swing.plaf.ColorUIResource; import javax.swing.plaf.metal.DefaultMetalTheme; import javax.sound.sampled.*; //import com.jamonapi.*; public class GrafreeD extends Applet implements ActionListener { static int RENDERME = 0; static boolean epsequal = false; static float epsvertex2 = 0.001f; static boolean linkUV = false; // not uniform at load time static Wav wav = new Wav("/Users/nbriere/0ut/wavs/monoformat"); // output"); static boolean hassound = false; static boolean savesound = false; public GrafreeD() { javax.swing.Timer timer = new javax.swing.Timer(1000, this); //timer.start(); // wav.read(); // .save(); // wav.play(); } static void Assert(boolean b) { if (!b) new Exception().printStackTrace(); // assert(b); } public void init() { add(openEditorButton = new Button("GrafreeD")); //Composite god = new Composite(); universe = new Composite(); universe.material = new cMaterial(); //god.addChild(universe); universe.name = "Applet"; theApplet3D = this; } /**/ public boolean action(Event evt, Object arg) { if (evt.target == openEditorButton) { universe.root = true; universe.openEditWindow(null, true); // , true); return true; } else { return super.action(evt, arg); } } /**/ //This method plays back audio data from an // audio file whose name is specified in the // text field. static PlayThread PlayAudio(String filename, double volume, double frequence) { try { PlayThread play = new PlayThread(); play.volume = volume; play.frequence = frequence; play.filename = filename; //Create a thread to play back the data and // start it running. It will run until the // end of file, or the Stop button is // clicked, whichever occurs first. // Because of the data buffers involved, // there will normally be a delay between // the click on the Stop button and the // actual termination of playback. play.start(); return play; } catch (Exception e) { e.printStackTrace(); return null; }//end catch }//end playAudio static PlayWordThread PlayWord(String word, double volume) { try { PlayWordThread play = new PlayWordThread(); play.volume = volume; play.words = word.split(" "); play.start(); return play; } catch (Exception e) { e.printStackTrace(); return null; }//end catch }//end playAudio //=============================================// //Inner class to play back the data from the // audio file. static class PlayThread extends Thread { String filename; byte tempbuffer[] = new byte[10000000]; short shortbuffer[] = new short[5000000]; short smoothbuffer[] = new short[5000000]; SourceDataLine sourceDataLine; AudioFormat audioFormat; AudioInputStream audioInputStream; double volume; double frequence; void Smooth(short[] buf) { short[] news = new short[buf.length]; System.arraycopy(buf, 0, news, 0, buf.length); for (int i=buf.length-1; --i>=1;) { news[i] = (short)(0.25*buf[i-1] + 0.5*buf[i] + 0.25*buf[i+1]); } System.arraycopy(news, 0, buf, 0, buf.length); } public void run() { try { while (true) // --count>=0) { File soundFile = new File(filename); audioInputStream = AudioSystem.getAudioInputStream(soundFile); audioFormat = audioInputStream.getFormat(); System.out.println(audioFormat); DataLine.Info dataLineInfo = new DataLine.Info( SourceDataLine.class, audioFormat); sourceDataLine = (SourceDataLine) AudioSystem.getLine( dataLineInfo); sourceDataLine.open(audioFormat); sourceDataLine.start(); int cnt; //Keep looping until the input read method // returns -1 for empty stream or the // user clicks the Stop button causing // stopPlayback to switch from false to // true. while ((cnt = audioInputStream.read( tempbuffer, 0, tempbuffer.length)) != -1) { for (int i=shortbuffer.length; --i>=0;) { int i2 = i*2; short value0 = (short)(tempbuffer[i2]&0xFF); short value1 = (short)(tempbuffer[i2+1]<<8); value0 *= volume; // seems useless... value1 *= volume; shortbuffer[i] = (short)(value0 | value1); // if (shortBuffer[i] != 0) // break; } System.arraycopy(shortbuffer, 0, smoothbuffer, 0, shortbuffer.length); // for (int loop=1000; --loop>=0;) // Smooth(smoothbuffer); // for (int loop=100; --loop>=0;) // Smooth(shortbuffer); // for (int i=shortbuffer.length; --i>=0;) // { // shortbuffer[i] -= smoothbuffer[i]; // } if (false) // frequence > 1) { // robot... // int base = 30; // for (int i=0; i*frequence=0;) { //shortBuffer[i] = (short)(shortBuffer[(int)(i*frequence)]); shortbuffer[i] = shortbuffer[i+1]; } } // for (int i=shortbuffer.length; --i>=0;) // { // shortbuffer[i] += smoothbuffer[i]; // } for (int i=shortbuffer.length; --i>=0;) { int i2 = i*2; byte tempBufferi2 = (byte)((shortbuffer[i]&0xFF) * 1); byte tempBufferi21 = (byte)(((shortbuffer[i]&0xFF00)>>8) * 1); // if (tempBufferi2 != tempBuffer[i2]) // break; // if (tempBufferi21 != tempBuffer[i2+1]) // break; tempbuffer[i2] = tempBufferi2; tempbuffer[i2+1] = tempBufferi21; } for (int loop=(int)frequence; --loop>=0;) if (cnt > 0) { //Write data to the internal buffer of // the data line where it will be // delivered to the speaker. sourceDataLine.write( tempbuffer, 0, cnt); }//end if }//end while //Block and wait for internal buffer of the // data line to empty. sourceDataLine.drain(); sourceDataLine.close(); } } catch (Exception e) { e.printStackTrace(); System.exit(0); }//end catch }//end run }//end inner class PlayThread static class PlayWordThread extends Thread { String[] words; byte tempbuffer[] = new byte[1000000]; byte tempbuffer1[] = new byte[1000000]; byte tempbuffer2[] = new byte[1000000]; byte tempbuffer3[] = new byte[1000000]; byte tempbuffer4[] = new byte[1000000]; byte tempbuffer5[] = new byte[1000000]; byte tempbuffer6[] = new byte[1000000]; byte temprotate[] = new byte[1000000]; SourceDataLine sourceDataLine; AudioFormat audioFormat; AudioInputStream audioInputStream; double volume; void Smooth(byte[] buf) { byte[] news = new byte[buf.length]; System.arraycopy(buf, 0, news, 0, buf.length); for (int i=buf.length-1; (i-=2)>=1;) { news[i] = (byte)(0.25*buf[i-1] + 0.5*buf[i] + 0.25*buf[i+1]); } System.arraycopy(news, 0, buf, 0, buf.length); } boolean IsZero(byte[] source) { for (int i=source.length; --i>=0;) { if (source[i] != 0) return false; } return true; } void Scale(byte[] source, double factor) { for (int i=source.length; --i>=0;) { source[i] *= factor; } } void Attract(byte[] source, byte[] target, int count) { double factor = 1; // Math.random() * 0.5; //if (factor > 0.25) // factor = 0.25; //if (factor > 0.25) // factor = 0.1; // 0.5; for (int i=count; --i>=0;) { int diff = target[i] - source[i]; source[i] += diff * factor; } } public void run() { try { while (true) { for (int wi=0; wi=0;) { int mod = (int)(Math.random() * 1); switch (mod) { case 0: Attract(tempbuffer, tempbuffer1, cnt); break; case 1: Attract(tempbuffer, tempbuffer2, cnt2); break; case 2: Attract(tempbuffer, tempbuffer3, cnt3); break; case 3: Attract(tempbuffer, tempbuffer4, cnt4); break; case 4: Attract(tempbuffer, tempbuffer5, cnt5); break; case 5: Attract(tempbuffer, tempbuffer6, cnt6); break; } sourceDataLine.write(tempbuffer, 0, cnt); } }//end while } java.util.Arrays.fill(tempbuffer1, (byte)0); while (!IsZero(tempbuffer)) { Attract(tempbuffer, tempbuffer1, mincount); // silence between words sourceDataLine.write( tempbuffer, 0, mincount); } Thread.sleep(1000); } //Block and wait for internal buffer of the // data line to empty. sourceDataLine.drain(); sourceDataLine.close(); sourceDataLine = null; Thread.sleep(3000); } } catch (Exception e) { e.printStackTrace(); System.exit(0); }//end catch }//end run }//end inner class PlayThread static java.util.Hashtable dictionary = new java.util.Hashtable(); static void InitDictionary() { try { // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream("/Users/nbriere/Documents/dictionary/cmudict.0.7a"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { // Print the content on the console if (strLine.charAt(0) == ';') continue; //System.out.println(strLine); String[] words = strLine.split(" "); if (dictionary.get(words[0]) != null) assert(dictionary.get(words[0]) == null); dictionary.put(words[0], words); } //Close the input stream in.close(); } catch (Exception e) {//Catch exception if any System.err.println("Error: " + e.getMessage()); } } static void Spell(String word) { PlayWord(word, 1); } public static void main(String argv[]) { if (false) // argv.length == 0) { String javaPath = System.getProperty("java.home") + "/bin/java"; java.net.URL u = new GrafreeD().getClass().getResource("default.png"); System.out.println(u); String jarfile = u.toString().split("file:")[1].split("default.png")[0]; String jarpath = u.toString().split("file:")[1].split("GrafreeD.jar")[0]; System.out.println(jarfile); System.out.println(jarpath); jarfile = jarfile.substring(0, jarfile.length() - 2); // -Djava.library.path=/Users/nbriere/Projects/shared/lib String command = "" + javaPath + " -Xmx1024m -Djava.library.path=" + jarpath + "lib -jar " + jarfile + " dummyarg"; try { System.out.println(command); Runtime.getRuntime().exec(command); return; } catch (Exception e) { e.printStackTrace(); } } // InitDictionary(); // Spell("is over".toUpperCase()); //Spell("horse word the growth".toUpperCase()); //Spell("breath beard of three days growth".toUpperCase()); //Spell("mannequin misinterpret accomplishment apologize".toUpperCase()); //Spell("Block and wait for internal buffer".toUpperCase()); //Spell("Keep looping until the input read method".toUpperCase()); //Spell("formula anything Do you catch the thing".toUpperCase()); // //Spell("I know what you mean It is truly incredible".toUpperCase()); if (false) try { // java.applet.AudioClip clip = java.applet.Applet.newAudioClip( // new java.net.URL("file:/Users/nbriere/Documents/normand.wav")); // clip.play(); // clip = java.applet.Applet.newAudioClip( // new java.net.URL("file:/Users/nbriere/Downloads/crowd-talking-9.wav")); // clip.play(); // for (int loop=1; --loop>=0;) // { // javax.sound.sampled.AudioInputStream ais = javax.sound.sampled.AudioSystem.getAudioInputStream(new java.net.URL("file:/Users/nbriere/Downloads/boarding-accouncement-1.wav")); // javax.sound.sampled.Clip clip = javax.sound.sampled.AudioSystem.getClip(); // clip.open(ais); // clip.start(); // Thread.sleep(1000); // } // ais = javax.sound.sampled.AudioSystem.getAudioInputStream(new java.net.URL("file:/Users/nbriere/Documents/Footsteps.wav")); // clip = javax.sound.sampled.AudioSystem.getClip(); // clip.open(ais); // clip.loop(10); // try{ File soundFile = new File("/Users/nbriere/Documents/testcrowdboard.wav"); javax.sound.sampled.AudioInputStream audioInputStream = javax.sound.sampled.AudioSystem.getAudioInputStream(soundFile); javax.sound.sampled.AudioFormat audioFormat = audioInputStream.getFormat(); System.out.println(audioFormat); javax.sound.sampled.DataLine.Info dataLineInfo = new javax.sound.sampled.DataLine.Info( javax.sound.sampled.SourceDataLine.class, audioFormat); javax.sound.sampled.SourceDataLine sourceDataLine = (javax.sound.sampled.SourceDataLine) javax.sound.sampled.AudioSystem.getLine( dataLineInfo); //Create a thread to play back the data and // start it running. It will run until the // end of file, or the Stop button is // clicked, whichever occurs first. // Because of the data buffers involved, // there will normally be a delay between // the click on the Stop button and the // actual termination of playback. // new PlayThread().start(); sourceDataLine.open(audioFormat); sourceDataLine.start(); byte tempBuffer[] = new byte[10000]; int cnt; //Keep looping until the input read method // returns -1 for empty stream or the // user clicks the Stop button causing // stopPlayback to switch from false to // true. while ((cnt = audioInputStream.read( tempBuffer, 0, tempBuffer.length)) != -1 // && stopPlayback == false ) { if (cnt > 0) { //Write data to the internal buffer of // the data line where it will be // delivered to the speaker. sourceDataLine.write( tempBuffer, 0, cnt); }//end if }//end while //Block and wait for internal buffer of the // data line to empty. sourceDataLine.drain(); sourceDataLine.close(); // }catch (Exception e) { // e.printStackTrace(); // System.exit(0); // }//end catch } catch (Exception murle) { System.out.println(murle); } //PlayAudio("/Users/nbriere/Downloads/dictionary/a.wav", 1, 10); // PlayAudio("/Users/nbriere/Downloads/boarding-accouncement-1.wav", 2, 1); // PlayAudio("/Users/nbriere/Documents/poche.wav", 4, 1); //PlayAudio("/Users/nbriere/Downloads/tone1k-22-8.wav", 1, 1); //PlayAudio("/Users/nbriere/Documents/ocean-wave-1.wav", 1,1); // PlayAudio("/Users/nbriere/Downloads/house-heater-1.wav", 0.5, 1); //PlayAudio("/Users/nbriere/Documents/Footsteps.wav", 10); // PlayAudio("/Users/nbriere/Downloads/Footsteps-1.wav", 1, 1); // try{Thread.sleep(5000);}catch(Exception e){} // PlayAudio("/Users/nbriere/Downloads/Footsteps-6.wav", 4, 1); try { MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme()); //MetalLookAndFeel.setCurrentTheme(new Theme(Constants.beigeTheme)); UIManager.setLookAndFeel(new MetalLookAndFeel()); } catch (Exception e) { } //Monitor mon=MonitorFactory.start("myFirstMonitor"); standAlone = true; theApplet3D = new GrafreeD(); theApplet3D.universe = new Composite(); theApplet3D.universe.name = "Scene"; theApplet3D.universe.material = new cMaterial(); // theApplet3D.universe.textures = CameraPane.DEFAULT_TEXTURE; theApplet3D.universe.root = true; theApplet3D.universe.openEditWindow(null, true); //, true); //mon.stop(); //System.out.println(mon); } // Timer callback public void actionPerformed(ActionEvent e) { if (universe != null) { //System.out.print("."); universe.invariants(); //System.gc(); } } static int depth = 0; static java.util.Stack stack = new java.util.Stack(); static boolean traceoff = false; // true; static float[] colorV = new float[5]; static void traceon() { traceoff = false; } static void traceoff() { traceoff = true; } static void tracein(Object m) { if (traceoff) { return; } tracein(m, ""); } static void tracein(Object call, Object data) { if (traceoff) { return; } stack.push(call); tabs(); System.err.println("" + call /*+ "-" + depth + " "*/ + data); tabs(); System.err.println("{"); depth++; } static void trace(Object call) { if (traceoff) { return; } tabs(); System.err.println("" + call /*+ "-" + depth + " "*/); } static void trace(Object call, Object data) { if (traceoff) { return; } tabs(); System.err.println("" + call /*+ "-" + depth + " "*/ + data); } static void traceout(Object call) { //traceout(); if (traceoff) { return; } depth--; tabs(); System.err.println("} // " + call /*+ "-" + depth + " "*/); stack.pop(); // assert == call } static void traceout(Object call, Object data) { //traceout(); if (traceoff) { return; } depth--; tabs(); System.err.println("} // " + call /*+ "-" + depth + " "*/ + data); stack.pop(); // assert == call } static void traceout() { if (traceoff) { return; } depth--; tabs(); System.err.println("} // " + stack.pop() /*+ "-" + depth + " "*/); } static void tabs() { for (int i = 0; i < depth; i++) { System.err.print(" "); } } static public void StackTrace() { (new Exception()).printStackTrace(System.err); } static public Object clone(Object o) { if (o == null) return null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(baos); out.writeObject(o); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream in = new ObjectInputStream(bais); Object obj = in.readObject(); in.close(); out.close(); return obj; } catch (Exception e) { System.err.println(e); return null; } } static int mix3(int a, int b, int c) { a -= b; a -= c; a ^= (c >> 13); b -= c; b -= a; b ^= (a << 8); c -= a; c -= b; c ^= (b >> 13); a -= b; a -= c; a ^= (c >> 12); b -= c; b -= a; b ^= (a << 16); c -= a; c -= b; c ^= (b >> 5); a -= b; a -= c; a ^= (c >> 3); b -= c; b -= a; b ^= (a << 10); c -= a; c -= b; c ^= (b >> 15); return c; } static void AnalyzeObject(Object abc) { for (java.lang.reflect.Field field : abc.getClass().getDeclaredFields()) { field.setAccessible(true); String name = field.getName(); Object value = null; try { value = field.get(abc); } catch(Exception e) { e.printStackTrace(); } System.out.printf("Field name / value : %s = %s\n", name, value); } } public static String loadStreamIntoString(InputStream stream) throws IOException { if (stream == null) { throw new java.io.IOException("null stream"); } stream = new java.io.BufferedInputStream(stream); int avail = stream.available(); byte[] data = new byte[avail]; int numRead = 0; int pos = 0; do { if (pos + avail > data.length) { byte[] newData = new byte[pos + avail]; System.arraycopy(data, 0, newData, 0, pos); data = newData; } numRead = stream.read(data, pos, avail); if (numRead >= 0) { pos += numRead; } avail = stream.available(); } while (avail > 0 && numRead >= 0); return new String(data, 0, pos, "US-ASCII"); } public static GrafreeD theApplet3D; public static boolean standAlone = true; public Composite universe; public static Object3D clipboard = new Object3D(); public static boolean clipboardIsTempGroup; private Button openEditorButton; }