float xmag=0,newXmag=0,ymag=0,newYmag=0; public TOKEN token = new TOKEN(); public int DRAW = 0, s_id=1,m_id = 2, h_id = 3,event; public int ts,tmin,th,id; public int mode =0; private int w=300 , h=300, clock=200, rate=30; private int countClick = 1; BImage a,b; BFont metaBold; public int zoomZ = 0,zoomY = 0,zoomX = 0; public String [] Voice = new String [4]; public SynthSample [] mySamp = new SynthSample [4]; public SynthTable synthTable; double basePitch; SampleReader_16V1 mySampler; InputStream stream; LineOut myOut; void setup() { SynthPort portA; Voice[0] = "twelve02.WAV"; Voice[1] = "second02.WAV"; Voice[2] = "minute02.WAV" ; Voice[3] = "hour02.WAV"; Synth.startEngine(0); try { for (int i=0;i<4;i++ ) { // in 006, use openStream stream = loadStream(Voice[i]); mySamp[i] = new SynthSampleWAV(); loadSample(mySamp[i],stream); } // Determine MIDI pitch associated with this sample. */ // basePitch = EqualTemperedTuning.getMIDIPitch( mySamp.getBaseFrequency() ); } catch (IOException e) {SynthAlert.showError(this,e); } Sim.init(hour()*3600+minute()*60+second(),Const.HEAP); /* Determine MIDI pitch associated with this sample. */ // basePitch = EqualTemperedTuning.getMIDIPitch( mySamp.getBaseFrequency() ); mySampler = new SampleReader_16V1(); myOut = new LineOut(); mySampler.output.connect( 0, myOut.input, 0 ); mySampler.output.connect( 0, myOut.input, 1 ); mySampler.start(); myOut.start(); size(300, 300); background(0); stroke(255,0,0); noFill(); a = loadImage("RomanClock.gif"); drawClock(); ts = second(); // Values from 0 - 59 tmin = minute(); // Values from 0 - 59 th = hour(); // Values from 0 - 23 Sim.schedule(s_id, 0 ,token); BFont metaBold; metaBold = loadFont("CenturySchbk.vlw.gz"); textFont(metaBold, 16); textMode(ALIGN_CENTER); } void loop() { background(0); fill(255,255,255); text("drag to rotate; click to re-center", width/2, 10+ height-height/10.0); text("press c,d,v,f,b or g to rotate on X,Y & Z axes ", width/2, 25+ height-height/10.0); if(mode==0) // mode 0 is the initial 3D mode, you can toggle from 2D to 3D by pressing either A or S { // initial position transformations translate(w/2, h/2,0); rotateX(PI/3); rotateZ(PI/4); // rotation accordint to the mouse mouvement rotateX(-ymag); rotateY(xmag); //rotation according to keys pressed on the keybord rotateY((zoomY)*PI/90); rotateZ((zoomZ)*PI/90); rotateX((zoomX)*PI/90); // drawClock(); draw3D(); drawSecond(); drawMinute(); drawHour(); } //draw 3D if(mode==1) // clock in 2D mode { drawClock2D(); drawSecond2D(); drawMinute2D(); drawHour2D(); } event = Sim.next_event(token,hour()*3600+minute()*60+second(),Const.SYNC); if (event >= 0) { if(event == s_id){ ts++; sound(1); if(ts%60 == 0) { Sim.schedule(m_id, 0 ,token); } Sim.schedule(s_id, 1 ,token); } // end of if Second if(event == m_id){ tmin++; sound(2); if(tmin%60 == 0) { sound(3); Sim.schedule(h_id, 0 ,token); } } if(event == h_id){ th++; } } } void sound(int choice){ NoteThread noteThread; noteThread = new NoteThread(); noteThread.startSound(choice); } void drawClock(){ stroke(0,0,255); noFill(); translate(w/2, h/2); ellipseMode(CENTER_DIAMETER); ellipse(0,0,clock,clock); } void drawSecond() // draws only the seconds hand in the corresponding plane { noStroke(); // light stuff here // light 1 g.lightKind[0] = DIFFUSE; g.lightX[0] = 0.0; g.lightY[0] = 0.0; g.lightZ[0] = 0.0 ; g.lightR[0] = 0.2; // a very low greenish tint g.lightG[0] = 0.5; g.lightB[0] = 0.2; // light 2 g.lightKind[1] = AMBIENT; g.lightX[1] = 0.0; g.lightY[1] = 0.0; g.lightZ[1] = 0.0; g.lightR[1] = 0.6; g.lightG[1] = 0.6; g.lightB[1] = 0.0; lights(); // lights are on fill(255,255,255); rotateZ(PI+ts*PI/30 ); translate(0,10+clock/2,0); sphereDetail(30); // the sphere's level of details , the more you have, the smoother it is, the more time it take to render sphere(8); // draw sphere translate(0,-10-clock/2,0); // undo transform (equivalent to pop() ) rotateZ(-(PI+ts*PI/30) ); noLights(); // to stop the light effect } void drawMinute() // draws only the minutes hand in the corresponding plane { fill(255,255,255); rotateX(PI/2+tmin*PI/30 ); translate(0,40,0); noLights(); fill(100,100,255); stroke(255,255,255); box(5,80,5); translate(0,-40,0); rotateX(-(PI/2+tmin*PI/30) ); } void drawHour() // draws only the hours hand in the corresponding plane { stroke(255,255,255); fill(255,100,255); rotateY(PI/2+PI+th*PI/6 ); translate(25,0,0); box(50,5,5); translate(-25,0,0); rotateY(-(PI/2+PI+th*PI/6) ); } void draw3D() // draws the borders of the clock in space { stroke(150,150,150); strokeWeight(1); fill(255,184,0); noFill(); //translate(100,100); int x = 5; for(int i = 0; i <15; i++){ stroke(50,50,50); // draws the seconds border in space as a collection of Polygons inside a loop beginShape(POLYGON); vertex(clock*sin(i)/2,clock*cos(i)/2,-5); vertex(clock*sin(i)/2,clock*cos(i)/2,5); vertex(clock*sin(i+1)/2,clock*cos(i+1)/2,-5); vertex(clock*sin(i+1)/2,clock*cos(i+1)/2,5); endShape(); // draws the minutes border in space as a collection of Polygons inside a loop // stroke(100,255,255); beginShape(POLYGON); vertex(-5,clock*sin(i)/2,clock*cos(i)/2); vertex(5,clock*sin(i)/2,clock*cos(i)/2); vertex(-5,clock*sin(i+1)/2,clock*cos(i+1)/2); vertex(5,clock*sin(i+1)/2,clock*cos(i+1)/2); endShape(); // draws the hours border in space as a collection of Polygons inside a loop // stroke(255,100,255); beginShape(POLYGON); vertex(clock*sin(i)/2,-5,clock*cos(i)/2); vertex(clock*sin(i)/2,5,clock*cos(i)/2); vertex(clock*sin(i+1)/2,-5,clock*cos(i+1)/2); vertex(clock*sin(i+1)/2,5,clock*cos(i+1)/2); endShape(); } // translate(-100,-100); // the yellow box that indicates 12:00 stroke(255,255,255); fill(255,255,100); rotateY(PI/2+PI ); translate(clock/2,0,0); box(10,8,8); translate(-clock/2,0,0); rotateY(-(PI/2+PI) ); // ------hour ticks stroke(255,255,255); fill(255,100,255); for(int i = 0; i < 12; i++){ rotateY(PI/2+PI+i*PI/6 ); translate(clock/2,0,0); box(10,3,3); translate(-clock/2,0,0); rotateY(-(PI/2+PI+i*PI/6) ); } //-------minute ticks fill(255,255,100); stroke(255,255,255); rotateX(PI/2 ); translate(0,clock/2,0); box(10,8,8); translate(0,-clock/2,0); rotateX(-(PI/2) ); fill(100,100,255); stroke(255,255,255); for(int i = 0; i <60 ; i++) { rotateX(PI+i*PI/30 ); translate(0,clock/2,0); box(3,10,3); translate(0,-clock/2,0); rotateX(-(PI+i*PI/30) ); } // second ticks fill(100,100,255); stroke(255,255,255); for(int i = 0; i< 60 ; i++) { noLights(); rotateZ(PI+i*PI/30 ); translate(0,clock/2,0); sphereDetail(1); sphere(1); translate(0,-clock/2,0); rotateZ(-(PI+i*PI/30) ); } } //------------ void drawClock2D(){ stroke(0,0,255); noFill(); translate(w/2, h/2); image(a, -clock/2,-clock/2,clock,clock); ellipseMode(CENTER_DIAMETER); ellipse(0,0,clock,clock); } void drawSecond2D(){ stroke(0,0,255); fill(255,184,0); rotate(PI+ts*PI/30 ); ellipseMode(CORNER); ellipse(0,clock/2,5,20); rotate(-(PI+ts*PI/30) ); } void drawMinute2D(){ stroke(0,0,255); fill(255,184,0); rotate(PI+tmin*PI/30 ); ellipseMode(CORNER); ellipse(-5,-5,10,clock/2); rotate(-(PI+tmin*PI/30) ); } void drawHour2D(){ stroke(0,0,255); fill(255,184,0); rotate(PI+th*PI/6 ); ellipseMode(CORNER); ellipse(-5,-5,10,clock/3.5f); rotate(-(PI+tmin*PI/6) ); } void mousePressed() { xmag = 0; ymag = 0; } void mouseDragged() { newXmag = 2.0*(mouseX - width/2)/(float) width; newYmag = 2.0*(mouseY - height/2)/ (float) height; float diff = xmag-newXmag; if (abs(diff) > 0.01) { xmag -= diff/4.0; } diff = ymag-newYmag; if (abs(diff) > 0.01) { ymag -= diff/4.0; } } void mouseReleased() { } void keyPressed(){ if(key =='a'||key=='A') mode=0; if(key =='s'||key=='S') mode=1; if(key =='c' || key == 'C') zoomZ += 1; if(key =='d' || key == 'D') zoomZ -= 1; if(key =='v' || key == 'V') zoomY += 1; if(key =='f' || key == 'F') zoomY -= 1; if(key =='b' || key == 'B') zoomX += 1; if(key =='g' || key == 'G') zoomX -= 1; } public void loadSample( SynthSample sample, InputStream stream ) throws IOException { sample.load( stream ); } /** Convert MIDI pitch from keyboard to a sample rate in Hz */ double convertPitchToRate( double pitch ) { double frequency = EqualTemperedTuning.getMIDIFrequency( pitch ); return mySamp[0].getSampleRate() * ( frequency / mySamp[0].getBaseFrequency() ); } // public class NoteThread extends Thread public class NoteThread { void startSound(int hand) { int jsynTime,sample; double thepitch; // double pitch = 60.0 - diameter; double [] pitch = new double [4]; pitch [0] = 70; pitch [1] = 70; pitch [2] = 40; pitch[3] = 60; if (hand ==0) { sample = 0; thepitch = pitch [0]; } else if (hand ==1) { sample = 1; thepitch = pitch[1]; } else if (hand ==2) { sample = 2; thepitch = pitch[2]; } else { sample = 3; thepitch = pitch[3]; } double srate = convertPitchToRate( thepitch ); mySampler.rate.set( srate ); jsynTime = Synth.getTickCount(); mySampler.samplePort.queueOn(jsynTime,mySamp[sample]); mySampler.samplePort.queueOff(jsynTime+400,mySamp[sample]); } }