// Chloe Chao // LetterSound.java // Sound out each letter as printed out. import java.applet.*; import java.awt.*; import java.io.*; import java.lang.*; import java.util.Vector; import java.util.StringTokenizer; import java.net.*; public class LetterSound extends Applet implements Runnable { // AudioClip sound; // Vector sounds; // sound alphabet Thread thread; // animation thread int sleepRate = 100; // sleep between redraw in milliseconds Button button; TextField textField; ImageCanvas imageCanvas; Graphics offg; Image displayImage; String toBeSaid; boolean FLAG = false; // just to debug String sourceString; StringTokenizer sourceFile; int UNIT, LEFT_MARGIN, TOP_MARGIN; int currx, curry; int lastWidth = 0; Vector alphabet; public static final int NW = 0; public static final int NE = 1; public static final int SE = 2; public static final int SW = 3; int maxFrames=0; int currFrameNum=0; CodeString currCS = null; public void init() { UNIT = 4; LEFT_MARGIN = 6; TOP_MARGIN = 30; alphabet = new Vector(); toBeSaid = new String(); // Draw the gui. Panel p = new Panel(); p.setLayout(new GridLayout(0,2)); makeTextField(); p.add("West", textField); makeButton(); p.add("East", button); BorderLayout b = new BorderLayout(); setLayout(b); add( "North", p ); makeImageCanvas(); add( "South", imageCanvas ); // Create the image displayImage = createImage(size().width,size().height); offg = displayImage.getGraphics(); offg.setColor( Color.black ); imageCanvas.setImage(displayImage); imageCanvas.repaint(); //Read in the homemade font file. fetch("http://acg.media.mit.edu/mas962/users/cchao/ps8/soundfontcode.txt"); // Load the sounds /*try { java.net.URL test = new java.net.URL(getDocumentBase(), "db.txt"); sourceString = (String) test.getContent(); sourceFile = new StringTokenizer(sourceString);//, " \t\n\r,;/\"("); } catch (Exception e) {} System.out.println("sourceFile is: "+sourceFile.countTokens() ); sounds = loadSounds( sourceFile ); */ //sounds = getSounds(); // Start the applet thread. thread = new Thread( this ); thread.start(); } // init public void start() { if (thread != null) { thread.resume(); } } // start public void stop() { if (thread != null) { thread.suspend(); } } //stop public void destroy() { if(thread != null) { thread.stop(); } } // destroy public void run() { try { while( true ) { offg = displayImage.getGraphics(); speak(); repaint(); thread.sleep(sleepRate); } } catch (InterruptedException e) {} } // run private void speak() { // Each sound less than a second. (1 sec=1000 milliseconds?) if( currFrameNum >= maxFrames ) { // The animation is over. currCS = null; maxFrames = 0; currFrameNum = 0; toBeSaid = new String(); // empty } // if else if( (currFrameNum%10 == 0) && ((currFrameNum/10)0 )//currx != LEFT_MARGIN && curry != TOP_MARGIN ) currx+=lastWidth; if( currx>(size().width-(12*UNIT)) ) { currx = LEFT_MARGIN; curry+=(12*UNIT); }// if // Go get the next letter. char currLetter = toBeSaid.charAt( (int)(currFrameNum/10) ); currCS = findCodeString( currLetter ); // Start playing the letter sound. if( currCS!=null ) { lastWidth = currCS.getWidth(); //System.out.println("I'm ready to play a sound!"); if( currCS.getSound()!=null ) { AudioClip audy = currCS.getSound(); audy.play(); //System.out.println("I've played a sound!"); }// if }// if }// end else if drawLetter(); currFrameNum++; } // speak private void drawLetter() { if( currCS!=null ) { int dim = UNIT*12; Image i2 = currCS.getImage(); Graphics g2 = i2.getGraphics(); g2.setColor( Color.white ); g2.fillRect( 0, 0, 2*dim, dim ); float val; if( (val=((currFrameNum%10)/10.0f))<0.8 ) g2.setColor( new Color( val, val, val) ); else g2.setColor( new Color( .7f, .7f, .7f ) ); for( int i=0; i': case '?': case '@': this.width = u*19; break; default: this.width = u*12; break; } } public void addSerif( Polygon p ) { serifs.addElement(p); } public char getCode() { return this.code; } public void setColor(Color col) { this.color = col; } public Color getColor() { return this.color; } public Image getImage() { return this.i; } public void setSound( AudioClip audio ) { this.sound = audio; } public AudioClip getSound() { return this.sound; } public int getWidth() { return this.width; } } // CodeString