home *** CD-ROM | disk | FTP | other *** search
/ Internet 1996 World Exposition / park.org.s3.amazonaws.com.7z / park.org.s3.amazonaws.com / Cdrom / Pavilions / BrainOpera / online / net-music / net-instrument / MMSharle.java < prev    next >
Encoding:
Java Source  |  2017-09-21  |  30.4 KB  |  889 lines

  1. import java.util.Hashtable;
  2. import java.util.Enumeration;
  3. import java.util.BitSet;
  4. import java.util.Date;
  5.  
  6. import java.io.*;
  7. import java.awt.*;
  8. import java.net.*;
  9. import java.applet.Applet;
  10.  
  11. import java.lang.Math;    // sin, cos
  12.  
  13. // Next time: extend everything off a static global class
  14.  
  15. import java.util.StringTokenizer;
  16. import java.util.Vector;
  17. import java.lang.Integer;
  18.  
  19. import java.net.*;
  20. import java.io.*;
  21. import java.applet.*;
  22. import java.awt.*;
  23.  
  24. public class MMSharle extends Applet implements Runnable, WebStarHandler
  25. {
  26.     Thread m_thread = null;
  27.     int m_iLayerNumber = -1;
  28.     int m_iMMUDRed=128, m_iMMUDGreen=128, m_iMMUDBlue=128;
  29.     int m_iBaseRed=128, m_iBaseGreen=128, m_iBaseBlue=128;
  30.     int m_x=0, m_y=0;    double m_fMM=0, m_fUD=0;
  31.     boolean m_bClearDots=false;
  32.     Image offscreenImage;
  33.     Graphics offscreenGraphics;
  34.  
  35.     RectButton m_rbMain, m_rbBlue, m_rbGreen, m_rbRed, m_rbViolet;
  36.     final static int g_iNumParams=16;
  37.     RectSlider marr_rsParams[];
  38.     
  39.     CMIDI m_midi;
  40.  
  41.     // Constants
  42.     int SECOND = 1000;
  43.     int BEATS_PER_SEC = 24;
  44.     int BEAT_TIME = (SECOND/BEATS_PER_SEC);
  45.  
  46.     static int g_iMinWidth = 40;
  47.     static double g_fBorderWidth = .15, g_fBorderHeight = .15;    // With respect to applet
  48.     static double g_f4ColorDim = .25;    // With respect to border
  49.     static double g_fSliderWidth = .15;    // With respect to distance b/w sliders
  50.  
  51.     static int g_i4ColorWidth = 20, g_iWidthSliders = 10;
  52.     static int g_iMinHeight = 10;
  53.     static int g_iWindowWidth;
  54.     static int g_iWindowHeight;
  55.     static int g_iMainWidth;
  56.  
  57.     // Globals
  58.     int g_iLastTime = -1;
  59.     int sm_iMIDIvolume = 64;
  60.  
  61.     final int BST_NO_NOTE=0;    // No note on this beat
  62.     final int BST_WHISPER=1;
  63.     final int BST_LIGHT=2;    // Faint note
  64.     final int BST_MEDIUM=3;    // Normal note
  65.     final int BST_HEAVY=4;    // Emphsized note
  66.     final int BST_BANG=5;
  67.     // constants
  68.     final static int DIM_COHESION=0;
  69.     final static int DIM_KEY=1;
  70.     final static int DIM_NUM_LAYERS=2;
  71.     final static int DIM_RTHM_CONSISTENCY=3;
  72.     final static int DIM_RTHM_DENSITY=4;
  73.     final static int DIM_RTHM_LENGTH=5;
  74.     final static int DIM_SCALE=6;
  75.     final static int DIM_SECTION_CHANGE=7;
  76.     final static int DIM_TEMPO=8;
  77.     // More constants
  78.     final static int LDIM_ACTIVE=0;
  79.     final static int LDIM_CONSONANCE=1;
  80.     final static int LDIM_DIRECTION=2;
  81.     final static int LDIM_INSTRUMENT=3;
  82.     final static int LDIM_PITCH_MAGNET=4;
  83.     final static int LDIM_RTHM_CONSISTENCY=5;
  84.     final static int LDIM_RTHM_DENSITY=6;
  85.     final static int LDIM_RTHM_LENGTH=7;
  86.     final static int LDIM_RTHM_STYLE=8;
  87.     final static int LDIM_RTHM_STYLE_ARG=9;
  88.     final static int LDIM_TEMPO=10;
  89.  
  90. void assert (boolean b) { if (!b) (new int[1])[-1] = 0; }
  91.  
  92. //---------------------------------------------
  93. public int iGetParameter (String str)
  94. { return (new Integer(getParameter(str))).intValue(); }
  95. //---------------------------------------------
  96.  
  97. public void initGUI ()
  98. {    //resize(g_iWindowWidth, g_iWindowHeight);
  99.     int iWidth = this.size().width;
  100.     int iHeight = this.size().height;
  101.     m_x = iWidth/2; m_y = iHeight/2;
  102.     m_fMM=0.5; m_fUD=0.5;
  103.  
  104.     offscreenImage = createImage(iWidth,iHeight);
  105.     offscreenGraphics = offscreenImage.getGraphics();
  106.  
  107.     if (iWidth > g_iMinWidth) g_iWindowWidth = iWidth;
  108.     else g_iWindowWidth = g_iMinWidth;
  109.     if (iHeight > g_iMinHeight) g_iWindowHeight = iHeight;
  110.     else g_iWindowHeight = g_iMinHeight;
  111.  
  112.     int iBorderWidth = (int)( (double)(g_iWindowWidth) * g_fBorderWidth + .5);
  113.     int iBorderHeight = (int)( (double)(g_iWindowHeight) * g_fBorderHeight + .5);
  114.  
  115.     m_rbMain = new RectButton(iBorderWidth, iBorderHeight,
  116.         g_iWindowWidth - 2*iBorderWidth, g_iWindowHeight - 2*iBorderHeight);
  117.     m_rbMain.m_iBorderWidth=2;
  118.  
  119.     int i4ColorWidth = (int)( (double)(iBorderWidth)*g_f4ColorDim + .5);
  120.     int i4ColorHeight = (int)( (double)(iBorderHeight)*g_f4ColorDim + .5);
  121.     m_rbBlue = new RectButton(m_rbMain.m_iXPos + m_rbMain.m_iWidth,
  122.         m_rbMain.m_iYPos + m_rbMain.m_iHeight, i4ColorWidth, i4ColorHeight);
  123.     m_rbBlue.m_cMain = new Color(120,120,255);
  124.     m_rbGreen=new RectButton(m_rbMain.m_iXPos + m_rbMain.m_iWidth,
  125.         m_rbMain.m_iYPos - i4ColorHeight, i4ColorWidth, i4ColorHeight);
  126.     m_rbGreen.m_cMain = new Color(120,255,120);
  127.     m_rbRed = new RectButton(m_rbMain.m_iXPos - i4ColorWidth,
  128.         m_rbMain.m_iYPos - i4ColorHeight, i4ColorWidth, i4ColorHeight);
  129.     m_rbRed.m_cMain = new Color(255,120,120);
  130.     m_rbViolet=new RectButton(m_rbMain.m_iXPos - i4ColorWidth,
  131.         m_rbMain.m_iYPos + m_rbMain.m_iHeight, i4ColorWidth, i4ColorHeight);
  132.     m_rbViolet.m_cMain = new Color(160,120,160);
  133.  
  134.     marr_rsParams = new RectSlider[g_iNumParams];
  135.     int iEnd = 0;
  136.     for (int iSide=0; iSide<4; iSide++) {
  137.         int iStart = iEnd;
  138.         iEnd = (iSide+1)*(g_iNumParams/4) + ((iSide<g_iNumParams%4)? iSide+1 : 0);
  139.         boolean bVert = iSide == 0 || iSide == 2;
  140.         boolean bBottomLeft = iSide == 0 || iSide == 1;
  141.         double fGapWidth; int iSliderWidth;
  142.         if (iSide == 0 || iSide == 2)
  143.             fGapWidth = (double)(m_rbMain.m_iWidth)/(double)(iEnd-iStart+1);
  144.         else fGapWidth = (double)(m_rbMain.m_iHeight)/(double)(iEnd-iStart+1);
  145.         iSliderWidth = (int)(fGapWidth * g_fSliderWidth + .5);
  146.         for (int C=iStart; C<iEnd; C++) {
  147.             switch (iSide) {
  148.             case 0:    marr_rsParams[C] = new RectSlider(
  149.                         (int)(iBorderWidth + (1+C-iStart)*fGapWidth - iSliderWidth/2), 0,
  150.                         iSliderWidth, iBorderHeight);
  151.                     break;
  152.             case 1: marr_rsParams[C] = new RectSlider(g_iWindowWidth - iBorderWidth,
  153.                         (int)(iBorderHeight + (1+C-iStart)*fGapWidth - iSliderWidth/2),
  154.                         iBorderWidth, iSliderWidth);
  155.                     break;
  156.             case 2: marr_rsParams[C] = new RectSlider(
  157.                         (int)(iBorderWidth + (iEnd-C)*fGapWidth - iSliderWidth/2),
  158.                         g_iWindowHeight - iBorderHeight,
  159.                         iSliderWidth, iBorderHeight);
  160.                     break;
  161.             case 3: marr_rsParams[C] = new RectSlider(0,
  162.                         (int)(iBorderHeight + (iEnd-C)*fGapWidth - iSliderWidth/2),
  163.                         iBorderWidth, iSliderWidth);
  164.                     break;
  165.             }
  166.             marr_rsParams[C].m_bVert = bVert;
  167.             marr_rsParams[C].m_bBottomLeft = bBottomLeft;
  168.             marr_rsParams[C].m_fValue = .5;
  169.         }
  170.     }
  171. }
  172.  
  173. //---------------------------------------------
  174.  
  175. public void init ()
  176. {    System.out.println("Initializing...");
  177.     
  178.     MMinit();
  179.     CMIDI.sm_applet = this;
  180.     m_midi = new CMIDI();
  181.     m_midi.OnInit();
  182.  
  183.     // Misc
  184.     CGenPoint.g_secTest = new CSection(this);
  185.     System.out.println("Done initializing...");
  186. }
  187.  
  188. public void start ()
  189. {    if (m_thread == null) {
  190.         m_thread = new Thread(this);
  191.         m_thread.start();
  192.     }
  193.     m_midi.OnStart();
  194. }
  195. public void stop ()
  196. {    m_thread.stop();
  197.     m_thread = null;
  198.     m_midi.OnStop();
  199.     MMstop();
  200. }
  201. public void run ()
  202. {    Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
  203.     initGUI();
  204.     MMrun();
  205.     long lTime = (new Date()).getTime();
  206.     LoopThis((int)lTime);
  207.     m_bClearDots = true; repaint();
  208.     ReadHTMLParams();
  209.     while (m_thread != null) {
  210.         lTime = (new Date()).getTime();
  211.         //showStatus("While " + lTime);
  212.         LoopThis((int)lTime);
  213.     }
  214. }
  215.  
  216. //-----------------------------------------
  217. void ReadHTMLEvent (int iEventNum)
  218. {    int iIndex = iGetParameter("event" + iEventNum);
  219.     int iArg = iGetParameter("event" + iEventNum + "Arg");
  220.  
  221.     System.out.print("Event" + iEventNum + ": ");
  222.     if (iIndex != -1)
  223.         ProcessInput(iIndex, (char)iArg);
  224.     else ChangeActiveLayer(iArg);
  225. }
  226.  
  227. public void ReadHTMLParams ()
  228. {    ProcessInput(17, (char)42);
  229.     for (int C=0; true; C++) {
  230.         try ReadHTMLEvent(C);
  231.         catch(NumberFormatException e)
  232.             return;    // If out of events, stop reading.
  233.     }
  234. }
  235.  
  236. //-----------------------
  237. void ChangeActiveLayer (int iLayerIndex)
  238. {    CStanza pstzCurrStanza = CGenPoint.g_secTest.pstzGetPrevStanza(1);
  239.     if (iLayerIndex >= pstzCurrStanza.iNumLayers()) {
  240.         m_iLayerNumber = pstzCurrStanza.iNumLayers();
  241.         CGenPoint.g_secTest.AppendLayerClone(pstzCurrStanza.plyrGetLayer(0));
  242.     } else m_iLayerNumber = iLayerIndex;
  243.     System.out.println("Current layer " +
  244.         m_iLayerNumber);
  245. }
  246.  
  247. //-----------------------
  248. public void LoopThis (int fTime)
  249. {    //showStatus("Loop " + (int)fTime);
  250.     char pvNoteNum, pvVolume, pvChannel;
  251.     int iNewTime = (int)(fTime)/BEAT_TIME;
  252.     if (g_iLastTime != -1 && iNewTime <= g_iLastTime)
  253.         return;
  254.     g_iLastTime = iNewTime;
  255.  
  256.     CNote pNote;
  257.     // Which notes to stop playing?
  258.     while ((pNote = CGenPoint.g_secTest.pNoteToEnd(g_iLastTime))
  259.         != null)
  260.     {    pvChannel = (char)m_midi.iGetInstrumentChannel(pNote.pvInstrument);
  261.         //tw.spr("off(%d,%d,%d,%d)\t", pvChannel, pNote.iNoteNum,
  262.         //    pNote.iBeatNum, pNote.iDuration);
  263.         m_midi.MidiOutNoteOFF(pNote.iNoteNum, pvChannel);
  264.     }
  265.  
  266.     boolean bPlayedNote = false;
  267.     // Which notes to start playing?
  268.     while ((pNote = CGenPoint.g_secTest.pNoteToPlay(g_iLastTime))
  269.         != null)
  270.     {
  271.         pvNoteNum = (char)pNote.iNoteNum;
  272.         switch (pNote.bstStress)
  273.         {    case BST_WHISPER: pvVolume = (char)(sm_iMIDIvolume/4); break;
  274.             case BST_LIGHT: pvVolume = (char)(sm_iMIDIvolume*3/4); break;
  275.             case BST_MEDIUM: pvVolume = (char)(sm_iMIDIvolume); break;
  276.             case BST_HEAVY: pvVolume = (char)((sm_iMIDIvolume*3 + 127)/4);
  277.                 break;
  278.             case BST_BANG: pvVolume = (char)((sm_iMIDIvolume + 127*3)/4);
  279.                 break;
  280.             default: pvVolume=0; System.out.println("Eh?"); break;
  281.         }
  282.         pvChannel = (char)m_midi.iGetInstrumentChannel(pNote.pvInstrument);
  283.         //tw.spr("note(%d,%d,%d,%d,%d)\r", pvChannel,
  284.         //    pvNoteNum, pvVolume, pNote.iBeatNum, pNote.iDuration);
  285.         //showStatus(g_iLastTime + " On " + (int)pvNoteNum);
  286.         m_midi.MidiOutNoteON(pvNoteNum, pvVolume, pvChannel, fTime);
  287.         bPlayedNote = true;
  288.     }
  289.     if (bPlayedNote || g_iLastTime % 12 == 0) {
  290.         repaint();
  291.     }
  292.     m_midi.LoopThis(fTime);
  293.     try { Thread.sleep(25); } catch(Exception e) {}
  294. }
  295.  
  296. //=====================================
  297.  
  298. char pvGetLastInput (int iParamNum)
  299. {    int iLyr = (m_iLayerNumber < 0) ? 0 : m_iLayerNumber;
  300.     switch (iParamNum) {
  301.     case 15: // no break;
  302.     case -1: return (char)m_iLayerNumber;
  303.     case 0:    return CGenPoint.pvGetCoord(DIM_COHESION);
  304.     case 1:    return CGenPoint.pvGetCoord(DIM_KEY);
  305.     case 2:    return CGenPoint.pvGetCoord(DIM_SCALE);
  306.     case 3:    return CGenPoint.pvGetCoord(DIM_TEMPO);
  307.     case 4: return CGenPoint.pvGetLayerCoord(LDIM_RTHM_CONSISTENCY, 0);
  308.     case 5: return CGenPoint.pvGetLayerCoord(LDIM_RTHM_DENSITY, 0);
  309.     case 6: return CGenPoint.pvGetLayerCoord(LDIM_RTHM_LENGTH, 0);
  310.     case 7: return CGenPoint.pvGetLayerCoord(LDIM_CONSONANCE, iLyr);
  311.     case 8: return CGenPoint.pvGetLayerCoord(LDIM_DIRECTION, iLyr);
  312.     case 9: return CGenPoint.pvGetLayerCoord(LDIM_INSTRUMENT, iLyr);
  313.     case 10: return CGenPoint.pvGetLayerCoord(LDIM_ACTIVE, iLyr);
  314.     case 11: return CGenPoint.pvGetLayerCoord(LDIM_RTHM_STYLE, iLyr);
  315.     case 12: return CGenPoint.pvGetLayerCoord(LDIM_RTHM_STYLE_ARG, iLyr);
  316.     case 13: // no break;
  317.     case 17: return CGenPoint.pvGetCoord(DIM_SECTION_CHANGE);
  318.     case 14: return CGenPoint.pvGetLayerCoord(LDIM_PITCH_MAGNET, iLyr);
  319.     default: assert(false);
  320.     }
  321.     return 0;
  322. }
  323.  
  324. void ProcessInput (int iParamNum, char pvInput)
  325. {System.out.println("Processing " + iParamNum + "," + (int)pvInput);
  326.     switch (iParamNum) {
  327.     case 0:    CGenPoint.iSetCoord(DIM_COHESION, pvInput); return;
  328.     case 1:    CGenPoint.iSetCoord(DIM_KEY, pvInput); return;
  329.     case 2:    CGenPoint.iSetCoord(DIM_SCALE, pvInput); return;
  330.     case 3:    CGenPoint.iSetCoord(DIM_TEMPO, pvInput); return;
  331.     case 4:    CGenPoint.iSetCoord(DIM_RTHM_CONSISTENCY, pvInput); return;
  332.     case 5:    CGenPoint.iSetCoord(DIM_RTHM_DENSITY, pvInput); return;
  333.     case 6:    CGenPoint.iSetCoord(DIM_RTHM_LENGTH, pvInput); return;
  334.     case 13: // no break;
  335.     case 17: CGenPoint.iSetCoord(DIM_SECTION_CHANGE, pvInput); return;
  336.     case 15: ChangeActiveLayer(pvInput); return;
  337.     }
  338.  
  339.     int iStart, iEnd;
  340.     if (m_iLayerNumber >= 0) {
  341.         iStart = m_iLayerNumber;
  342.         iEnd = m_iLayerNumber;
  343.         if (iParamNum == 10 && pvInput == 0 && iStart==0) iStart++;    // First layer always active
  344.     } else {
  345.         iStart = 0;
  346.         iEnd = CGenPoint.g_secTest.pstzGetPrevStanza(1).iNumLayers() - 1;
  347.         if (iParamNum == 10 && pvInput == 0) iStart++;    // First layer always active
  348.     }
  349.     for (int C=iStart; C<=iEnd; C++) {
  350.         switch (iParamNum) {
  351.         case 7: CGenPoint.iSetLayerCoord(LDIM_CONSONANCE, C, pvInput); break;
  352.         case 8: CGenPoint.iSetLayerCoord(LDIM_DIRECTION, C, pvInput); break;
  353.         case 9: CGenPoint.iSetLayerCoord(LDIM_INSTRUMENT, C, pvInput); break;
  354.         case 10: CGenPoint.iSetLayerCoord(LDIM_ACTIVE, C, pvInput); break;
  355.         case 11: CGenPoint.iSetLayerCoord(LDIM_RTHM_STYLE, C, pvInput);
  356.             CGenPoint.iSetLayerCoord(LDIM_RTHM_STYLE_ARG, C, (char)64);
  357.             break;
  358.         case 12: CGenPoint.iSetLayerCoord(LDIM_RTHM_STYLE_ARG, C, pvInput); break;
  359.         case 14: CGenPoint.iSetLayerCoord(LDIM_PITCH_MAGNET, C, pvInput); break;
  360.         default: assert(false);
  361.         }
  362.     }
  363. }
  364.  
  365. //=================================================
  366.  
  367. void OnButtonBlue()
  368. {    showStatus("Blue");
  369.     m_iBaseRed=120;m_iBaseGreen=120;m_iBaseBlue=255;
  370.     m_iMMUDRed=120;m_iMMUDGreen=120;m_iMMUDBlue=255;
  371.     m_x = m_rbMain.m_iXPos + m_rbMain.m_iWidth;
  372.     m_y = m_rbMain.m_iYPos + m_rbMain.m_iHeight;
  373.     m_fMM = 1; m_fUD = 1;
  374.     ProcessInput(0, (char)30);    // Cohesion
  375.     ProcessInput(1, (char)61);    // Key
  376.     ProcessInput(2, (char)50);    // Scale
  377.     ProcessInput(3, (char)105);    // Tempo
  378.     ProcessInput(4, (char)10);    // R. Consistency
  379.     ProcessInput(5, (char)17);    // R. Density
  380.     ProcessInput(6, (char)50);    // R. Length
  381.     ChangeActiveLayer(0);
  382.     ProcessInput(7, (char)74);    // Consonance
  383.     ProcessInput(8, (char)50);    // Direction
  384.     // 20.ReedOrgan 53:Oohs 72:Piccolo 49.StrEns2
  385.     ProcessInput(9, (char)m_midi.iGetDefaultInst(5, this));    // Instrument
  386.     ProcessInput(10, (char)1);    // Active
  387.     ProcessInput(11, (char)3);    // R. Style
  388.     ProcessInput(12, (char)0);    // R. Style Arg
  389.     ProcessInput(14, (char)43);    // Pitch Magnet
  390.     for (int C=1; C<(int)CGenPoint.pvGetCoord(DIM_NUM_LAYERS); C++) {
  391.         ChangeActiveLayer(C);
  392.         ProcessInput(10, (char)0);    // Inactive
  393.     }
  394.     m_bClearDots = true; repaint();
  395. }
  396.  
  397. void OnButtonGreen() 
  398. {    showStatus("Green");
  399.     m_iBaseRed=120;m_iBaseGreen=255;m_iBaseBlue=120;
  400.     m_iMMUDRed=120;m_iMMUDGreen=255;m_iMMUDBlue=120;
  401.     m_x = m_rbMain.m_iXPos + m_rbMain.m_iWidth;
  402.     m_y = m_rbMain.m_iYPos;
  403.     m_fMM = 1; m_fUD = 0;
  404.     ProcessInput(0, (char)64);    // Cohesion
  405.     ProcessInput(1, (char)72);    // Key
  406.     ProcessInput(2, (char)10);    // Scale
  407.     ProcessInput(3, (char)81);    // Tempo
  408.     ProcessInput(4, (char)50);    // R. Consistency
  409.     ProcessInput(5, (char)8);    // R. Density
  410.     ProcessInput(6, (char)25);    // R. Length
  411.     ChangeActiveLayer(0);    // 1st Layer...
  412.     ProcessInput(7, (char)64);    // Consonance
  413.     ProcessInput(8, (char)54);    // Direction
  414.     // 50:SynthStr1 97:Soundtrack
  415.     ProcessInput(9, (char)m_midi.iGetDefaultInst(3, this));    // Instrument
  416.     ProcessInput(10, (char)1);    // Active
  417.     ProcessInput(11, (char)3);    // R. Style
  418.     ProcessInput(12, (char)0);    // R. Style Arg
  419.     ProcessInput(14, (char)65);    // Pitch Magnet
  420.     ChangeActiveLayer(1);    // 2nd Layer...
  421.     ProcessInput(7, (char)94);    // Consonance
  422.     ProcessInput(8, (char)32);    // Direction
  423.     // 94:Halo 102:Echoes
  424.     ProcessInput(9, (char)m_midi.iGetDefaultInst(4, this));    // Instrument
  425.     ProcessInput(10, (char)1);    // Active
  426.     ProcessInput(11, (char)1);    // R. Style
  427.     ProcessInput(12, (char)64);    // R. Style Arg
  428.     ProcessInput(14, (char)84);    // Pitch Magnet
  429.     /*if (m_sliLayer.GetRangeMax() <= 1)
  430.         m_sliLayer.SetRangeMax(2, TRUE);    // Update slider
  431.     else*/ for (int C=2; C<(int)CGenPoint.pvGetCoord(DIM_NUM_LAYERS); C++) {
  432.         ChangeActiveLayer(C);
  433.         ProcessInput(10, (char)0);    // Inactive
  434.     }
  435.     m_bClearDots = true; repaint();
  436. }
  437.  
  438. void OnButtonRed() 
  439. {    showStatus("Red");
  440.     m_iBaseRed=255;m_iBaseGreen=120;m_iBaseBlue=120;
  441.     m_iMMUDRed=255;m_iMMUDGreen=120;m_iMMUDBlue=120;
  442.     m_x = m_rbMain.m_iXPos;
  443.     m_y = m_rbMain.m_iYPos;
  444.     m_fMM = 0; m_fUD = 0;
  445.     ProcessInput(0, (char)80);    // Cohesion
  446.     ProcessInput(1, (char)60);    // Key
  447.     ProcessInput(2, (char)30);    // Scale
  448.     ProcessInput(3, (char)23);    // Tempo
  449.     ProcessInput(4, (char)90);    // R. Consistency
  450.     ProcessInput(5, (char)64);    // R. Density
  451.     ProcessInput(6, (char)15);    // R. Length
  452.     ChangeActiveLayer(0);    // 1st Layer...
  453.     ProcessInput(7, (char)54);    // Consonance
  454.     ProcessInput(8, (char)54);    // Direction
  455.     // 96:Rain 25:GuitSteel
  456.     ProcessInput(9, (char)m_midi.iGetDefaultInst(0, this));    // Instrument
  457.     ProcessInput(10, (char)1);    // Active
  458.     ProcessInput(11, (char)3);    // R. Style
  459.     ProcessInput(12, (char)0);    // R. Style Arg
  460.     ProcessInput(14, (char)72);    // Pitch Magnet
  461.     ChangeActiveLayer(1);    // 2nd Layer...
  462.     ProcessInput(7, (char)94);    // Consonance
  463.     ProcessInput(8, (char)64);    // Direction
  464.     // 88:NewAge
  465.     ProcessInput(9, (char)m_midi.iGetDefaultInst(1, this));    // Instrument
  466.     ProcessInput(10, (char)1);    // Active
  467.     ProcessInput(11, (char)0);    // R. Style
  468.     ProcessInput(12, (char)0);    // R. Style Arg
  469.     ProcessInput(14, (char)48);    // Pitch Magnet
  470.     ChangeActiveLayer(2);    // 3rd Layer...
  471.     ProcessInput(7, (char)94);    // Consonance
  472.     ProcessInput(8, (char)54);    // Direction
  473.     // 127:GunShot 117:TaikoDrum
  474.     ProcessInput(9, (char)m_midi.iGetDefaultInst(2, this));    // Instrument
  475.     ProcessInput(10, (char)1);    // Active
  476.     ProcessInput(11, (char)3);    // R. Style
  477.     ProcessInput(12, (char)0);    // R. Style Arg
  478.     ProcessInput(14, (char)55);    // Pitch Magnet
  479.     /*if (m_sliLayer.GetRangeMax() <= 2)
  480.         m_sliLayer.SetRangeMax(3, TRUE);    // Update slider
  481.     else*/ for (int C=3; C<(int)CGenPoint.pvGetCoord(DIM_NUM_LAYERS); C++) {
  482.         ChangeActiveLayer(C);
  483.         ProcessInput(10, (char)0);    // Inactive
  484.     }
  485.     m_bClearDots = true; repaint();
  486. }
  487.  
  488. void OnButtonViolet() 
  489. {    showStatus("Violet (cyu@mit.edu v5.0)");
  490.     m_iBaseRed=160;m_iBaseGreen=120;m_iBaseBlue=160;
  491.     m_iMMUDRed=160;m_iMMUDGreen=120;m_iMMUDBlue=160;
  492.     m_x = m_rbMain.m_iXPos;
  493.     m_y = m_rbMain.m_iYPos + m_rbMain.m_iHeight;
  494.     m_fMM = 0; m_fUD = 1;
  495.     //m_sliMM.SetPos(0);
  496.     ProcessInput(0, (char)20);    // Cohesion
  497.     ProcessInput(1, (char)59);    // Key
  498.     ProcessInput(2, (char)79);    // Scale
  499.     ProcessInput(3, (char)31);    // Tempo
  500.     ProcessInput(4, (char)10);    // R. Consistency
  501.     ProcessInput(5, (char)80);    // R. Density
  502.     ProcessInput(6, (char)20);    // R. Length
  503.     ChangeActiveLayer(0);    // 1st Layer...
  504.     ProcessInput(7, (char)44);    // Consonance
  505.     ProcessInput(8, (char)60);    // Direction
  506.     // 30.DistGuit 39:SynthBass2
  507.     ProcessInput(9, (char)m_midi.iGetDefaultInst(6, this));    // Instrument
  508.     ProcessInput(10, (char)1);    // Active
  509.     ProcessInput(11, (char)3);    // R. Style
  510.     ProcessInput(12, (char)0);    // R. Style Arg
  511.     ProcessInput(14, (char)48);    // Pitch Magnet
  512.     ChangeActiveLayer(1);    // 1nd Layer...
  513.     ProcessInput(7, (char)64);    // Consonance
  514.     ProcessInput(8, (char)30);    // Direction
  515.     // 114:SteelDrum 100.Bright
  516.     ProcessInput(9, (char)m_midi.iGetDefaultInst(7, this));    // Instrument
  517.     ProcessInput(10, (char)1);    // Active
  518.     ProcessInput(11, (char)2);    // R. Style
  519.     ProcessInput(12, (char)42);    // R. Style Arg
  520.     ProcessInput(14, (char)64);    // Pitch Magnet
  521.     ChangeActiveLayer(2);    // 3rd Layer...
  522.     ProcessInput(7, (char)64);    // Consonance
  523.     ProcessInput(8, (char)44);    // Direction
  524.     // 124:TeleRing 115:Woodblock 118:SynthDrum
  525.     ProcessInput(9, (char)m_midi.iGetDefaultInst(8, this));    // Instrument
  526.     ProcessInput(10, (char)1);    // Active
  527.     ProcessInput(11, (char)0);    // R. Style
  528.     ProcessInput(12, (char)0);    // R. Style Arg
  529.     ProcessInput(14, (char)72);    // Pitch Magnet
  530.     /*if (m_sliLayer.GetRangeMax() <= 2)
  531.         m_sliLayer.SetRangeMax(3, TRUE);    // Update slider
  532.     else*/ for (int C=3; C<(int)CGenPoint.pvGetCoord(DIM_NUM_LAYERS); C++) {
  533.         ChangeActiveLayer(C);
  534.         ProcessInput(10, (char)0);    // Inactive
  535.     }
  536.     m_bClearDots = true; repaint();
  537. }
  538.  
  539. int i24Scale (double f01, double f10, int i00, int i01, int i10, int i11)
  540. {
  541.     int iRange0001 = i01 - i00;
  542.     int iRange1011 = i11 - i10;
  543.     double fNewRange = (double)(iRange0001) +
  544.         f10*(double)(iRange1011 - iRange0001);
  545.     double fNewStart = (double)(i00) + f10*(double)(i10 - i00);
  546.     return (int)(fNewStart + f01*fNewRange);
  547. }
  548.  
  549. void OnMMUD(double fMM, double fUD)
  550. {    showStatus ("MM " + fMM + ", UD " + fUD);
  551.     ProcessInput(0, (char)i24Scale(fMM,fUD, 80,64,20,30));    // Cohesion
  552.     ProcessInput(2, (char)(int)(fUD*79));    // Scale
  553.     ProcessInput(3, (char)i24Scale(fMM,fUD, 23,81,31,105));    // Tempo
  554.     ProcessInput(4, (char)i24Scale(fMM,fUD, 90,50,10,10));    // R. Consistency
  555.     ProcessInput(5, (char)i24Scale(fMM,fUD, 64,8,80,17));    // R. Density
  556.     ProcessInput(6, (char)i24Scale(fMM,fUD, 15,25,20,50));    // R. Length
  557.     ChangeActiveLayer(-1);
  558.     m_iMMUDRed = i24Scale(fMM,fUD, 255, 120, 160, 120);
  559.     m_iMMUDGreen = i24Scale(fMM,fUD, 120, 255, 120, 120);
  560.     m_iMMUDBlue = i24Scale(fMM,fUD, 120, 120, 160, 255);
  561.     CGenPoint.g_secTest.m_bMutateUpdate = true; repaint();
  562.     //ProcessInput(7, i24Scale(fMM,fUD, 64,64,44,74));    // Consonance
  563.     //UpdateSliderVals();
  564. }
  565.  
  566. //=============================================
  567.  
  568. public boolean MMmouseDown(int x, int y)
  569. {    if (m_rbMain.bMouseOver(x,y)) {
  570.         m_x = x; m_y = y;
  571.         m_fMM = (double)(x-m_rbMain.m_iXPos)/(double)(m_rbMain.m_iWidth);
  572.         m_fUD = (double)(y-m_rbMain.m_iYPos)/(double)m_rbMain.m_iHeight;
  573.         OnMMUD(m_fMM, m_fUD);
  574.     }
  575.     else if (m_rbBlue.bMouseOver(x,y)) OnButtonBlue();
  576.     else if (m_rbGreen.bMouseOver(x,y)) OnButtonGreen();
  577.     else if (m_rbRed.bMouseOver(x,y)) OnButtonRed();
  578.     else if (m_rbViolet.bMouseOver(x,y)) OnButtonViolet();
  579.     else for (int C=0; C<g_iNumParams; C++)
  580.         if (marr_rsParams[C].bMouseOver(x,y)) {
  581.             CGenPoint.g_secTest.m_bMutateUpdate = true; repaint();
  582.             int iVal = (int)(marr_rsParams[C].m_fValue*127);
  583.             switch (C) {
  584.             case 10: iVal = iVal>64 ? 1 : 0; break;
  585.             case 11: iVal /= 33; break;
  586.             case 15: iVal = (int)(.5 + marr_rsParams[C].m_fValue *
  587.                 (CGenPoint.g_secTest.pstzGetPrevStanza(1).iNumLayers()));
  588.             }
  589.             ProcessInput(C, (char)iVal);
  590.             break;
  591.         }
  592.     return true;
  593. }
  594. //-----------------------
  595. /*public boolean mouseDrag (java.awt.Event evt, int x, int y)
  596. {
  597.     if (m_rbMain.bMouseOver(x,y)) {
  598.         m_x = x; m_y = y;
  599.         m_fMM = (double)(x-m_rbMain.m_iXPos)/(double)(m_rbMain.m_iWidth);
  600.         m_fUD = (double)(y-m_rbMain.m_iYPos)/(double)m_rbMain.m_iHeight;
  601.         if (rand()%20 == 0) // Usually, don't update music.
  602.             mouseDown(evt, x, y);
  603.     }
  604.     return true;
  605. }
  606. */
  607. //-----------------------
  608. public void update (Graphics g)
  609. {    paintDots(offscreenGraphics);
  610.     if (m_bClearDots || CGenPoint.g_secTest.m_bMutateUpdate)
  611.         paint(offscreenGraphics);
  612.     g.drawImage(offscreenImage, 0, 0, this);
  613.     if (m_bRepaintPointer) {
  614.         drawpointer(g);
  615.         m_bRepaintPointer = false;
  616.     }
  617. }
  618. public void paint (Graphics g)
  619. {    //Color cMain = new Color((m_iBaseRed+m_iMMUDRed)/2,
  620.     //    (m_iBaseGreen+m_iMMUDGreen)/2, (m_iBaseBlue+m_iMMUDBlue)/2);
  621.     Color cBorder = new Color(m_iBaseRed,m_iBaseGreen,m_iBaseBlue);
  622.     if (m_bClearDots) {
  623.         //m_rbMain.paint(g, Color.black, Color.black);
  624.         g.setColor(Color.black);
  625.         g.fillRect(0,0,this.size().width,this.size().height);
  626.     }
  627.     m_rbBlue.paint(g, null, cBorder);
  628.     m_rbGreen.paint(g, null, cBorder);
  629.     m_rbRed.paint(g, null, cBorder);
  630.     m_rbViolet.paint(g, null, cBorder);
  631.     try {
  632.         if (m_bClearDots || CGenPoint.g_secTest.m_bMutateUpdate) {
  633.             for (int C=0; C<g_iNumParams; C++) {
  634.                 switch (C) {
  635.                 case 10: marr_rsParams[C].m_fValue = pvGetLastInput(C); break;
  636.                 case 11: marr_rsParams[C].m_fValue = (double)(pvGetLastInput(C))/3.0; break;
  637.                 case 15: marr_rsParams[C].m_fValue = (double)(m_iLayerNumber+1) /
  638.                     (1+CGenPoint.g_secTest.pstzGetPrevStanza(1).iNumLayers()); break;
  639.                 default: marr_rsParams[C].m_fValue = (double)(pvGetLastInput(C))/127.0; break;
  640.                 }
  641.                 marr_rsParams[C].paint(g, cBorder, Color.black);
  642.                 //if (C==3) m_x += (marr_rsParams[C].m_fValue - .5)*10;
  643.                 //if (C==2) m_y += (marr_rsParams[C].m_fValue - .5)*10;
  644.             }
  645.         } else for (int C=0; C<g_iNumParams; C++) {
  646.             marr_rsParams[C].paint(g, cBorder, Color.black);
  647.         }
  648.     } catch(Exception e);
  649.     m_bClearDots = false;
  650.     CGenPoint.g_secTest.m_bMutateUpdate = false;
  651. }
  652.  
  653. public int rand() { return (int)(Math.random() * 9747); }
  654. public void paintDots (Graphics g)
  655. {    boolean bBlack = rand()%4 == 0;
  656.     int iArmArc = 628 / ((m_x+m_y) % 8 + 1);
  657.     int iOffset = (m_x + m_y)%iArmArc;
  658.     for (int C=0; C<25; C++) {
  659.         int iDist = rand() % (1 + rand() % (m_rbMain.m_iWidth/4));
  660.         float fRadians = (float)(rand() % 628);
  661.         iDist = (int)((float)(iDist) * (float)((int)(fRadians+iOffset) % iArmArc)/(float)(iArmArc));
  662.         fRadians /= (float)(100);
  663.         int x = (int)(Math.sin(fRadians)*iDist + m_x);
  664.         int y = (int)(Math.cos(fRadians)*iDist + m_y);
  665.         if (bBlack) {
  666.             g.setColor(Color.black);
  667.             g.drawLine(x,y, x+1,y);
  668.             continue;
  669.         }
  670.         int r = i24Scale(m_fMM,m_fUD, 255, 120, 160, 120);
  671.         int gn = i24Scale(m_fMM,m_fUD, 120, 255, 120, 120);
  672.         int b = i24Scale(m_fMM,m_fUD, 120, 120, 160, 255);
  673.         //COLORREF cr = dcBmp.GetPixel(x,y);
  674.         Color cr = new Color((r+rand()%256)/2, (gn+rand()%256)/2, (b+rand()%256)/2);
  675.         g.setColor(cr);
  676.         g.drawLine(x,y, x+1,y);
  677.     }
  678. }
  679. //-------------------------------------------------
  680.     int MyX, MyY;                // for drawing the pointer
  681.     Color MyColor;
  682.  
  683.     StarClient sc;
  684.     int myStarID;                   
  685.     int port;
  686.  
  687.     boolean empowered;           // passed in through parameters
  688.     Vector dudes;                /* tracking number of users */
  689.     String MyName = "mud";
  690.  
  691.     boolean m_bRepaintPointer = false;
  692.  
  693. public void MMinit()
  694. {
  695.     try { port = Integer.parseInt(getParameter("CHANNEL")); }
  696.     catch (NumberFormatException e) { port = 8122; }
  697.     if ((port < 8100) || (port > 8500)) port = 8122; // default port
  698.     
  699.      // find out if we are impotent
  700.     String power = getParameter("POWER");
  701.     if (power == null) power = "yes";
  702.     if (power.equals("no")) empowered = false;
  703.     else empowered = true;
  704.  
  705.     sc = new StarClient(this);
  706.     dudes      = new Vector();
  707.     MyX = MyY = -100;             /* Start the marker off-screen */
  708.     MyColor = Color.red;
  709. }
  710.  
  711. public void MMstop ()
  712. {    if (sc !=null) sc.stop(); 
  713. }
  714.  
  715. public boolean mouseDown(java.awt.Event evt, int x, int y) 
  716. {
  717.       feedback();
  718.       if (empowered) {
  719.         /* transmit click coordinates */
  720.         String msg = " " + x + " " + y + " " + "red" + " " + MyName;
  721.         sc.send(msg);
  722.         
  723.         MyX = x; MyY = y;
  724.         m_bRepaintPointer = true;
  725.         return MMmouseDown(x,y);
  726.       }
  727.       return true;
  728. }
  729.  
  730.   /* Tell user how many are connected */
  731.   public void feedback() {        
  732.     String m;
  733.     if (MyName.equals("")) m = "";
  734.     else m = "(" + MyName + ")";
  735.     if (dudes.size() <= 1)
  736.       System.out.println("You're the only one here. " + m);
  737.     else
  738.       System.out.println("There are " + dudes.size() + " people connected. " + m);
  739.   }
  740.   /* Hello, network. I'm alive. */
  741.   public void beacon() {
  742.     String msg = " -100 -100 " + "red" + " " + MyName;  
  743.     sc.send(msg);
  744.   }
  745.   public void MMrun()
  746.     {
  747.       System.out.println("Multimouse by Jeff Breidenbach (c) 1996 v1.0beta2");
  748.       sc.start(getDocumentBase().getHost(), port);
  749.     }
  750.   /*************************/
  751.   /*** STAR CLIENT STUFF ***/
  752.   /*************************/
  753.  
  754.   /* called when the connection's all set  */
  755.   public void WS_up(String msg, Object who) 
  756.     {
  757.       myStarID = sc.getStarID();
  758.       showStatus(msg);
  759.     }
  760.   
  761.   /* called when the connection's down */
  762.   public void WS_down(String msg, Object who) 
  763.     {
  764.       showStatus("The connection is broken!");
  765.     }
  766.  
  767.   /* general messages and errors */
  768.   public void WS_debugLog(String msg, Object who) 
  769.     {
  770.       showStatus(msg);
  771.     }
  772.   /* gets called after reception of a new msg  */
  773.   public void WS_newMsg(String msg, Object who) 
  774.     {
  775.       int FromID, x, y, deadID;
  776.       boolean Understood;
  777.       String tok; 
  778.       String name="";
  779.       String col="red";
  780.       
  781.       FromID = -1; Understood = false; x = y = -100;
  782.       StringTokenizer st = new StringTokenizer(msg);
  783.       
  784.       /* who is it from?.. */
  785.       if(st.hasMoreTokens()) 
  786.         FromID = (Integer.valueOf(st.nextToken())).intValue();
  787.  
  788.       /* Is it a special annoucement (i.e. from the server itself) */
  789.       if(FromID == 0)
  790.         { 
  791.           if(st.hasMoreTokens()) 
  792.             {
  793.               tok = st.nextToken();
  794.               if( (tok.equals("OBITUARY"))&&(st.hasMoreTokens()) )
  795.                 {
  796.                   deadID = (Integer.valueOf(st.nextToken())).intValue();
  797.                   dudes.removeElement(new Integer(deadID));
  798.                   feedback();
  799.                 }
  800.             }
  801.           return;
  802.           }
  803.       
  804.       /* ..or from a web client? -> interpreted as an (x,y) position */
  805.       if(FromID > 0)
  806.         {
  807.           if(st.hasMoreTokens()) 
  808.             {
  809.               x = (Integer.valueOf(st.nextToken())).intValue();
  810.               if(st.hasMoreTokens()) 
  811.                 {
  812.                   y = (Integer.valueOf(st.nextToken())).intValue();
  813.                   Understood = true;
  814.                   if(st.hasMoreTokens()) 
  815.                     {
  816.                         col = st.nextToken();
  817.                       } 
  818.                   while (st.hasMoreTokens()) 
  819.                     {
  820.                       name = name + st.nextToken() + " ";
  821.                     }   
  822.                 }
  823.             }
  824.         }
  825.       
  826.       /* Let's do something about it */
  827.       if(Understood)
  828.         {
  829.           tracker(FromID, name); 
  830.           if(FromID == myStarID) {        /* Was it from Myself?      */ 
  831.           }             
  832.           else {                          /* ..or from somebody else? */
  833.             if (onscreen(x,y)) 
  834.               {
  835.                 MyX = x; MyY = y;          /* draw pointer */   
  836.                 m_bRepaintPointer = true;
  837.                 MMmouseDown(x,y);
  838.               }     
  839.           }
  840.         }  
  841.     }
  842.  
  843.   /* keep track of who's connected  */
  844.   public void tracker(int ID, String name) {             
  845.     Object thisdude  = new Integer(ID);
  846.     if (dudes.contains(thisdude)) {}        /* I know this guy... */
  847.     else {                                  /* found a new dude!  */
  848.       dudes.addElement(thisdude);           /* let's remember him */
  849.       beacon();                             /* Hello, new dude */ 
  850.       feedback();                           /* Hey, look! */
  851.     }
  852.   }
  853.   
  854.   /* is this mouseclick onscreen?  */
  855.   public boolean onscreen(int x, int y) {  
  856.     boolean answer;
  857.     if ((x != -100) || (y != -100)) answer = true; 
  858.     else answer = false;
  859.     return answer;
  860.   }
  861.   
  862.   /**********************/
  863.   /*** GRAPHICS STUFF ***/
  864.   /**********************/  
  865.   
  866.   public void drawpointer(Graphics g) {
  867.     int a = 8;                   /* adjust this for size */
  868.     int b = 2*a;        
  869.     int x1, x2, y1, y2;
  870.     Polygon p = new Polygon();
  871.     int h = this.size().height;
  872.     int w = this.size().width;
  873.  
  874.     if (onscreen (MyX, MyY)) {
  875.       if (MyX < (w-b)) {x1 = MyX+b; x2 = MyX+a;} else {x1 = MyX-b; x2 = MyX-a;}
  876.       if (MyY < (h-b)) {y1=MyY+a; y2=MyY+b;} else {y1 = MyY-a; y2 = MyY-b;}
  877.       p.addPoint(MyX, MyY);
  878.       p.addPoint(x1, y1);
  879.       p.addPoint(x2, y2);
  880.       p.addPoint(MyX, MyY);
  881.       g.setColor(MyColor);
  882.       g.fillPolygon(p);
  883.       g.setColor(Color.black);
  884.       g.drawPolygon(p);
  885.     }
  886.   }
  887.  
  888. }    // end class Sharle
  889.