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 / Sharle.java < prev    next >
Encoding:
Java Source  |  2017-09-21  |  24.0 KB  |  667 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. public class Sharle extends Applet implements Runnable
  16. {
  17.     Thread m_thread = null;
  18.     int m_iLayerNumber = -1;
  19.     int m_iMMUDRed=128, m_iMMUDGreen=128, m_iMMUDBlue=128;
  20.     int m_iBaseRed=128, m_iBaseGreen=128, m_iBaseBlue=128;
  21.     int m_x=0, m_y=0;    double m_fMM=0, m_fUD=0;
  22.     boolean m_bClearDots=false;
  23.     Image offscreenImage;
  24.     Graphics offscreenGraphics;
  25.  
  26.     RectButton m_rbMain, m_rbBlue, m_rbGreen, m_rbRed, m_rbViolet;
  27.     final static int g_iNumParams=16;
  28.     RectSlider marr_rsParams[];
  29.     
  30.     CMIDI m_midi;
  31.  
  32.     // Constants
  33.     int SECOND = 1000;
  34.     int BEATS_PER_SEC = 24;
  35.     int BEAT_TIME = (SECOND/BEATS_PER_SEC);
  36.  
  37.     static int g_iMinWidth = 40;
  38.     static double g_fBorderWidth = .15, g_fBorderHeight = .15;    // With respect to applet
  39.     static double g_f4ColorDim = .25;    // With respect to border
  40.     static double g_fSliderWidth = .15;    // With respect to distance b/w sliders
  41.  
  42.     static int g_i4ColorWidth = 20, g_iWidthSliders = 10;
  43.     static int g_iMinHeight = 10;
  44.     static int g_iWindowWidth;
  45.     static int g_iWindowHeight;
  46.     static int g_iMainWidth;
  47.  
  48.     // Globals
  49.     int g_iLastTime = -1;
  50.     int sm_iMIDIvolume = 64;
  51.  
  52.     final int BST_NO_NOTE=0;    // No note on this beat
  53.     final int BST_WHISPER=1;
  54.     final int BST_LIGHT=2;    // Faint note
  55.     final int BST_MEDIUM=3;    // Normal note
  56.     final int BST_HEAVY=4;    // Emphsized note
  57.     final int BST_BANG=5;
  58.     // constants
  59.     final static int DIM_COHESION=0;
  60.     final static int DIM_KEY=1;
  61.     final static int DIM_NUM_LAYERS=2;
  62.     final static int DIM_RTHM_CONSISTENCY=3;
  63.     final static int DIM_RTHM_DENSITY=4;
  64.     final static int DIM_RTHM_LENGTH=5;
  65.     final static int DIM_SCALE=6;
  66.     final static int DIM_SECTION_CHANGE=7;
  67.     final static int DIM_TEMPO=8;
  68.     // More constants
  69.     final static int LDIM_ACTIVE=0;
  70.     final static int LDIM_CONSONANCE=1;
  71.     final static int LDIM_DIRECTION=2;
  72.     final static int LDIM_INSTRUMENT=3;
  73.     final static int LDIM_PITCH_MAGNET=4;
  74.     final static int LDIM_RTHM_CONSISTENCY=5;
  75.     final static int LDIM_RTHM_DENSITY=6;
  76.     final static int LDIM_RTHM_LENGTH=7;
  77.     final static int LDIM_RTHM_STYLE=8;
  78.     final static int LDIM_RTHM_STYLE_ARG=9;
  79.     final static int LDIM_TEMPO=10;
  80.  
  81. void assert (boolean b) { if (!b) (new int[1])[-1] = 0; }
  82.  
  83. //---------------------------------------------
  84. public int iGetParameter (String str)
  85. { return (new Integer(getParameter(str))).intValue(); }
  86. //---------------------------------------------
  87.  
  88. public void initGUI ()
  89. {    //resize(g_iWindowWidth, g_iWindowHeight);
  90.     int iWidth = this.size().width;
  91.     int iHeight = this.size().height;
  92.     m_x = iWidth/2; m_y = iHeight/2;
  93.     m_fMM=0.5; m_fUD=0.5;
  94.  
  95.     offscreenImage = createImage(iWidth,iHeight);
  96.     offscreenGraphics = offscreenImage.getGraphics();
  97.  
  98.     if (iWidth > g_iMinWidth) g_iWindowWidth = iWidth;
  99.     else g_iWindowWidth = g_iMinWidth;
  100.     if (iHeight > g_iMinHeight) g_iWindowHeight = iHeight;
  101.     else g_iWindowHeight = g_iMinHeight;
  102.  
  103.     int iBorderWidth = (int)( (double)(g_iWindowWidth) * g_fBorderWidth + .5);
  104.     int iBorderHeight = (int)( (double)(g_iWindowHeight) * g_fBorderHeight + .5);
  105.  
  106.     m_rbMain = new RectButton(iBorderWidth, iBorderHeight,
  107.         g_iWindowWidth - 2*iBorderWidth, g_iWindowHeight - 2*iBorderHeight);
  108.     m_rbMain.m_iBorderWidth=2;
  109.  
  110.     int i4ColorWidth = (int)( (double)(iBorderWidth)*g_f4ColorDim + .5);
  111.     int i4ColorHeight = (int)( (double)(iBorderHeight)*g_f4ColorDim + .5);
  112.     m_rbBlue = new RectButton(m_rbMain.m_iXPos + m_rbMain.m_iWidth,
  113.         m_rbMain.m_iYPos + m_rbMain.m_iHeight, i4ColorWidth, i4ColorHeight);
  114.     m_rbBlue.m_cMain = new Color(120,120,255);
  115.     m_rbGreen=new RectButton(m_rbMain.m_iXPos + m_rbMain.m_iWidth,
  116.         m_rbMain.m_iYPos - i4ColorHeight, i4ColorWidth, i4ColorHeight);
  117.     m_rbGreen.m_cMain = new Color(120,255,120);
  118.     m_rbRed = new RectButton(m_rbMain.m_iXPos - i4ColorWidth,
  119.         m_rbMain.m_iYPos - i4ColorHeight, i4ColorWidth, i4ColorHeight);
  120.     m_rbRed.m_cMain = new Color(255,120,120);
  121.     m_rbViolet=new RectButton(m_rbMain.m_iXPos - i4ColorWidth,
  122.         m_rbMain.m_iYPos + m_rbMain.m_iHeight, i4ColorWidth, i4ColorHeight);
  123.     m_rbViolet.m_cMain = new Color(160,120,160);
  124.  
  125.     marr_rsParams = new RectSlider[g_iNumParams];
  126.     int iEnd = 0;
  127.     for (int iSide=0; iSide<4; iSide++) {
  128.         int iStart = iEnd;
  129.         iEnd = (iSide+1)*(g_iNumParams/4) + ((iSide<g_iNumParams%4)? iSide+1 : 0);
  130.         boolean bVert = iSide == 0 || iSide == 2;
  131.         boolean bBottomLeft = iSide == 0 || iSide == 1;
  132.         double fGapWidth; int iSliderWidth;
  133.         if (iSide == 0 || iSide == 2)
  134.             fGapWidth = (double)(m_rbMain.m_iWidth)/(double)(iEnd-iStart+1);
  135.         else fGapWidth = (double)(m_rbMain.m_iHeight)/(double)(iEnd-iStart+1);
  136.         iSliderWidth = (int)(fGapWidth * g_fSliderWidth + .5);
  137.         for (int C=iStart; C<iEnd; C++) {
  138.             switch (iSide) {
  139.             case 0:    marr_rsParams[C] = new RectSlider(
  140.                         (int)(iBorderWidth + (1+C-iStart)*fGapWidth - iSliderWidth/2), 0,
  141.                         iSliderWidth, iBorderHeight);
  142.                     break;
  143.             case 1: marr_rsParams[C] = new RectSlider(g_iWindowWidth - iBorderWidth,
  144.                         (int)(iBorderHeight + (1+C-iStart)*fGapWidth - iSliderWidth/2),
  145.                         iBorderWidth, iSliderWidth);
  146.                     break;
  147.             case 2: marr_rsParams[C] = new RectSlider(
  148.                         (int)(iBorderWidth + (iEnd-C)*fGapWidth - iSliderWidth/2),
  149.                         g_iWindowHeight - iBorderHeight,
  150.                         iSliderWidth, iBorderHeight);
  151.                     break;
  152.             case 3: marr_rsParams[C] = new RectSlider(0,
  153.                         (int)(iBorderHeight + (iEnd-C)*fGapWidth - iSliderWidth/2),
  154.                         iBorderWidth, iSliderWidth);
  155.                     break;
  156.             }
  157.             marr_rsParams[C].m_bVert = bVert;
  158.             marr_rsParams[C].m_bBottomLeft = bBottomLeft;
  159.             marr_rsParams[C].m_fValue = .5;
  160.         }
  161.     }
  162. }
  163.  
  164. //---------------------------------------------
  165.  
  166. public void init ()
  167. {    System.out.println("Initializing...");
  168.     
  169.     CMIDI.sm_applet = this;
  170.     m_midi = new CMIDI();
  171.     m_midi.OnInit();
  172.  
  173.     // Misc
  174.     CGenPoint.g_secTest = new CSection(this);
  175.     System.out.println("Done initializing...");
  176. }
  177.  
  178. public void start ()
  179. {    if (m_thread == null) {
  180.         m_thread = new Thread(this);
  181.         m_thread.start();
  182.     }
  183.     m_midi.OnStart();
  184. }
  185. public void stop ()
  186. {    m_thread.stop();
  187.     m_thread = null;
  188.     m_midi.OnStop();
  189. }
  190. public void run ()
  191. {    Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
  192.     initGUI();
  193.     long lTime = (new Date()).getTime();
  194.     LoopThis((int)lTime);
  195.     m_bClearDots = true; repaint();
  196.     ReadHTMLParams();
  197.     while (m_thread != null) {
  198.         lTime = (new Date()).getTime();
  199.         //showStatus("While " + lTime);
  200.         try LoopThis((int)lTime);
  201.         catch (Exception e);
  202.     }
  203. }
  204.  
  205. //-----------------------------------------
  206. void ReadHTMLEvent (int iEventNum)
  207. {    int iIndex = iGetParameter("event" + iEventNum);
  208.     int iArg = iGetParameter("event" + iEventNum + "Arg");
  209.  
  210.     System.out.print("Event" + iEventNum + ": ");
  211.     if (iIndex != -1)
  212.         ProcessInput(iIndex, (char)iArg);
  213.     else ChangeActiveLayer(iArg);
  214. }
  215.  
  216. public void ReadHTMLParams ()
  217. {    ProcessInput(17, (char)42);
  218.     for (int C=0; true; C++) {
  219.         try ReadHTMLEvent(C);
  220.         catch(NumberFormatException e)
  221.             return;    // If out of events, stop reading.
  222.     }
  223. }
  224.  
  225. //-----------------------
  226. void ChangeActiveLayer (int iLayerIndex)
  227. {    CStanza pstzCurrStanza = CGenPoint.g_secTest.pstzGetPrevStanza(1);
  228.     if (iLayerIndex >= pstzCurrStanza.iNumLayers()) {
  229.         m_iLayerNumber = pstzCurrStanza.iNumLayers();
  230.         CGenPoint.g_secTest.AppendLayerClone(pstzCurrStanza.plyrGetLayer(0));
  231.     } else m_iLayerNumber = iLayerIndex;
  232.     System.out.println("Current layer " +
  233.         m_iLayerNumber);
  234. }
  235.  
  236. //-----------------------
  237. public void LoopThis (int fTime)
  238. {    //showStatus("Loop " + (int)fTime);
  239.     char pvNoteNum, pvVolume, pvChannel;
  240.     int iNewTime = (int)(fTime)/BEAT_TIME;
  241.     if (g_iLastTime != -1 && iNewTime <= g_iLastTime)
  242.         return;
  243.     g_iLastTime = iNewTime;
  244.  
  245.     CNote pNote;
  246.     // Which notes to stop playing?
  247.     while ((pNote = CGenPoint.g_secTest.pNoteToEnd(g_iLastTime))
  248.         != null)
  249.     {    pvChannel = (char)m_midi.iGetInstrumentChannel(pNote.pvInstrument);
  250.         //tw.spr("off(%d,%d,%d,%d)\t", pvChannel, pNote.iNoteNum,
  251.         //    pNote.iBeatNum, pNote.iDuration);
  252.         m_midi.MidiOutNoteOFF(pNote.iNoteNum, pvChannel);
  253.     }
  254.  
  255.     boolean bPlayedNote = false;
  256.     // Which notes to start playing?
  257.     while ((pNote = CGenPoint.g_secTest.pNoteToPlay(g_iLastTime))
  258.         != null)
  259.     {
  260.         pvNoteNum = (char)pNote.iNoteNum;
  261.         switch (pNote.bstStress)
  262.         {    case BST_WHISPER: pvVolume = (char)(sm_iMIDIvolume/4); break;
  263.             case BST_LIGHT: pvVolume = (char)(sm_iMIDIvolume*3/4); break;
  264.             case BST_MEDIUM: pvVolume = (char)(sm_iMIDIvolume); break;
  265.             case BST_HEAVY: pvVolume = (char)((sm_iMIDIvolume*3 + 127)/4);
  266.                 break;
  267.             case BST_BANG: pvVolume = (char)((sm_iMIDIvolume + 127*3)/4);
  268.                 break;
  269.             default: pvVolume=0; System.out.println("Eh?"); break;
  270.         }
  271.         pvChannel = (char)m_midi.iGetInstrumentChannel(pNote.pvInstrument);
  272.         //tw.spr("note(%d,%d,%d,%d,%d)\r", pvChannel,
  273.         //    pvNoteNum, pvVolume, pNote.iBeatNum, pNote.iDuration);
  274.         //showStatus(g_iLastTime + " On " + (int)pvNoteNum);
  275.         m_midi.MidiOutNoteON(pvNoteNum, pvVolume, pvChannel, fTime);
  276.         bPlayedNote = true;
  277.     }
  278.     if (bPlayedNote || g_iLastTime % 12 == 0) {
  279.         repaint();
  280.     }
  281.     m_midi.LoopThis(fTime);
  282.     try { Thread.sleep(25); } catch(Exception e) {}
  283. }
  284.  
  285. //=====================================
  286.  
  287. char pvGetLastInput (int iParamNum)
  288. {    int iLyr = (m_iLayerNumber < 0) ? 0 : m_iLayerNumber;
  289.     switch (iParamNum) {
  290.     case 15: // no break;
  291.     case -1: return (char)m_iLayerNumber;
  292.     case 0:    return CGenPoint.pvGetCoord(DIM_COHESION);
  293.     case 1:    return CGenPoint.pvGetCoord(DIM_KEY);
  294.     case 2:    return CGenPoint.pvGetCoord(DIM_SCALE);
  295.     case 3:    return CGenPoint.pvGetCoord(DIM_TEMPO);
  296.     case 4: return CGenPoint.pvGetLayerCoord(LDIM_RTHM_CONSISTENCY, 0);
  297.     case 5: return CGenPoint.pvGetLayerCoord(LDIM_RTHM_DENSITY, 0);
  298.     case 6: return CGenPoint.pvGetLayerCoord(LDIM_RTHM_LENGTH, 0);
  299.     case 7: return CGenPoint.pvGetLayerCoord(LDIM_CONSONANCE, iLyr);
  300.     case 8: return CGenPoint.pvGetLayerCoord(LDIM_DIRECTION, iLyr);
  301.     case 9: return CGenPoint.pvGetLayerCoord(LDIM_INSTRUMENT, iLyr);
  302.     case 10: return CGenPoint.pvGetLayerCoord(LDIM_ACTIVE, iLyr);
  303.     case 11: return CGenPoint.pvGetLayerCoord(LDIM_RTHM_STYLE, iLyr);
  304.     case 12: return CGenPoint.pvGetLayerCoord(LDIM_RTHM_STYLE_ARG, iLyr);
  305.     case 13: // no break;
  306.     case 17: return CGenPoint.pvGetCoord(DIM_SECTION_CHANGE);
  307.     case 14: return CGenPoint.pvGetLayerCoord(LDIM_PITCH_MAGNET, iLyr);
  308.     default: assert(false);
  309.     }
  310.     return 0;
  311. }
  312.  
  313. void ProcessInput (int iParamNum, char pvInput)
  314. {System.out.println("Processing " + iParamNum + "," + (int)pvInput);
  315.     switch (iParamNum) {
  316.     case 0:    CGenPoint.iSetCoord(DIM_COHESION, pvInput); return;
  317.     case 1:    CGenPoint.iSetCoord(DIM_KEY, pvInput); return;
  318.     case 2:    CGenPoint.iSetCoord(DIM_SCALE, pvInput); return;
  319.     case 3:    CGenPoint.iSetCoord(DIM_TEMPO, pvInput); return;
  320.     case 4:    CGenPoint.iSetCoord(DIM_RTHM_CONSISTENCY, pvInput); return;
  321.     case 5:    CGenPoint.iSetCoord(DIM_RTHM_DENSITY, pvInput); return;
  322.     case 6:    CGenPoint.iSetCoord(DIM_RTHM_LENGTH, pvInput); return;
  323.     case 13: // no break;
  324.     case 17: CGenPoint.iSetCoord(DIM_SECTION_CHANGE, pvInput); return;
  325.     case 15: ChangeActiveLayer(pvInput); return;
  326.     }
  327.  
  328.     int iStart, iEnd;
  329.     if (m_iLayerNumber >= 0) {
  330.         iStart = m_iLayerNumber;
  331.         iEnd = m_iLayerNumber;
  332.         if (iParamNum == 10 && pvInput == 0 && iStart==0) iStart++;    // First layer always active
  333.     } else {
  334.         iStart = 0;
  335.         iEnd = CGenPoint.g_secTest.pstzGetPrevStanza(1).iNumLayers() - 1;
  336.         if (iParamNum == 10 && pvInput == 0) iStart++;    // First layer always active
  337.     }
  338.     for (int C=iStart; C<=iEnd; C++) {
  339.         switch (iParamNum) {
  340.         case 7: CGenPoint.iSetLayerCoord(LDIM_CONSONANCE, C, pvInput); break;
  341.         case 8: CGenPoint.iSetLayerCoord(LDIM_DIRECTION, C, pvInput); break;
  342.         case 9: CGenPoint.iSetLayerCoord(LDIM_INSTRUMENT, C, pvInput); break;
  343.         case 10: CGenPoint.iSetLayerCoord(LDIM_ACTIVE, C, pvInput); break;
  344.         case 11: CGenPoint.iSetLayerCoord(LDIM_RTHM_STYLE, C, pvInput);
  345.             CGenPoint.iSetLayerCoord(LDIM_RTHM_STYLE_ARG, C, (char)64);
  346.             break;
  347.         case 12: CGenPoint.iSetLayerCoord(LDIM_RTHM_STYLE_ARG, C, pvInput); break;
  348.         case 14: CGenPoint.iSetLayerCoord(LDIM_PITCH_MAGNET, C, pvInput); break;
  349.         default: assert(false);
  350.         }
  351.     }
  352. }
  353.  
  354. //=================================================
  355.  
  356. void OnButtonBlue()
  357. {    showStatus("Blue");
  358.     m_iBaseRed=120;m_iBaseGreen=120;m_iBaseBlue=255;
  359.     m_iMMUDRed=120;m_iMMUDGreen=120;m_iMMUDBlue=255;
  360.     m_x = m_rbMain.m_iXPos + m_rbMain.m_iWidth;
  361.     m_y = m_rbMain.m_iYPos + m_rbMain.m_iHeight;
  362.     m_fMM = 1; m_fUD = 1;
  363.     ProcessInput(0, (char)30);    // Cohesion
  364.     ProcessInput(1, (char)61);    // Key
  365.     ProcessInput(2, (char)50);    // Scale
  366.     ProcessInput(3, (char)105);    // Tempo
  367.     ProcessInput(4, (char)10);    // R. Consistency
  368.     ProcessInput(5, (char)17);    // R. Density
  369.     ProcessInput(6, (char)50);    // R. Length
  370.     ChangeActiveLayer(0);
  371.     ProcessInput(7, (char)74);    // Consonance
  372.     ProcessInput(8, (char)50);    // Direction
  373.     // 20.ReedOrgan 53:Oohs 72:Piccolo 49.StrEns2
  374.     ProcessInput(9, (char)m_midi.iGetDefaultInst(5, this));    // Instrument
  375.     ProcessInput(10, (char)1);    // Active
  376.     ProcessInput(11, (char)3);    // R. Style
  377.     ProcessInput(12, (char)0);    // R. Style Arg
  378.     ProcessInput(14, (char)43);    // Pitch Magnet
  379.     for (int C=1; C<(int)CGenPoint.pvGetCoord(DIM_NUM_LAYERS); C++) {
  380.         ChangeActiveLayer(C);
  381.         ProcessInput(10, (char)0);    // Inactive
  382.     }
  383.     m_bClearDots = true; repaint();
  384. }
  385.  
  386. void OnButtonGreen() 
  387. {    showStatus("Green");
  388.     m_iBaseRed=120;m_iBaseGreen=255;m_iBaseBlue=120;
  389.     m_iMMUDRed=120;m_iMMUDGreen=255;m_iMMUDBlue=120;
  390.     m_x = m_rbMain.m_iXPos + m_rbMain.m_iWidth;
  391.     m_y = m_rbMain.m_iYPos;
  392.     m_fMM = 1; m_fUD = 0;
  393.     ProcessInput(0, (char)64);    // Cohesion
  394.     ProcessInput(1, (char)72);    // Key
  395.     ProcessInput(2, (char)10);    // Scale
  396.     ProcessInput(3, (char)81);    // Tempo
  397.     ProcessInput(4, (char)50);    // R. Consistency
  398.     ProcessInput(5, (char)8);    // R. Density
  399.     ProcessInput(6, (char)25);    // R. Length
  400.     ChangeActiveLayer(0);    // 1st Layer...
  401.     ProcessInput(7, (char)64);    // Consonance
  402.     ProcessInput(8, (char)54);    // Direction
  403.     // 50:SynthStr1 97:Soundtrack
  404.     ProcessInput(9, (char)m_midi.iGetDefaultInst(3, this));    // Instrument
  405.     ProcessInput(10, (char)1);    // Active
  406.     ProcessInput(11, (char)3);    // R. Style
  407.     ProcessInput(12, (char)0);    // R. Style Arg
  408.     ProcessInput(14, (char)65);    // Pitch Magnet
  409.     ChangeActiveLayer(1);    // 2nd Layer...
  410.     ProcessInput(7, (char)94);    // Consonance
  411.     ProcessInput(8, (char)32);    // Direction
  412.     // 94:Halo 102:Echoes
  413.     ProcessInput(9, (char)m_midi.iGetDefaultInst(4, this));    // Instrument
  414.     ProcessInput(10, (char)1);    // Active
  415.     ProcessInput(11, (char)1);    // R. Style
  416.     ProcessInput(12, (char)64);    // R. Style Arg
  417.     ProcessInput(14, (char)84);    // Pitch Magnet
  418.     /*if (m_sliLayer.GetRangeMax() <= 1)
  419.         m_sliLayer.SetRangeMax(2, TRUE);    // Update slider
  420.     else*/ for (int C=2; C<(int)CGenPoint.pvGetCoord(DIM_NUM_LAYERS); C++) {
  421.         ChangeActiveLayer(C);
  422.         ProcessInput(10, (char)0);    // Inactive
  423.     }
  424.     m_bClearDots = true; repaint();
  425. }
  426.  
  427. void OnButtonRed() 
  428. {    showStatus("Red");
  429.     m_iBaseRed=255;m_iBaseGreen=120;m_iBaseBlue=120;
  430.     m_iMMUDRed=255;m_iMMUDGreen=120;m_iMMUDBlue=120;
  431.     m_x = m_rbMain.m_iXPos;
  432.     m_y = m_rbMain.m_iYPos;
  433.     m_fMM = 0; m_fUD = 0;
  434.     ProcessInput(0, (char)80);    // Cohesion
  435.     ProcessInput(1, (char)60);    // Key
  436.     ProcessInput(2, (char)30);    // Scale
  437.     ProcessInput(3, (char)23);    // Tempo
  438.     ProcessInput(4, (char)90);    // R. Consistency
  439.     ProcessInput(5, (char)64);    // R. Density
  440.     ProcessInput(6, (char)15);    // R. Length
  441.     ChangeActiveLayer(0);    // 1st Layer...
  442.     ProcessInput(7, (char)54);    // Consonance
  443.     ProcessInput(8, (char)54);    // Direction
  444.     // 96:Rain 25:GuitSteel
  445.     ProcessInput(9, (char)m_midi.iGetDefaultInst(0, this));    // Instrument
  446.     ProcessInput(10, (char)1);    // Active
  447.     ProcessInput(11, (char)3);    // R. Style
  448.     ProcessInput(12, (char)0);    // R. Style Arg
  449.     ProcessInput(14, (char)72);    // Pitch Magnet
  450.     ChangeActiveLayer(1);    // 2nd Layer...
  451.     ProcessInput(7, (char)94);    // Consonance
  452.     ProcessInput(8, (char)64);    // Direction
  453.     // 88:NewAge
  454.     ProcessInput(9, (char)m_midi.iGetDefaultInst(1, this));    // Instrument
  455.     ProcessInput(10, (char)1);    // Active
  456.     ProcessInput(11, (char)0);    // R. Style
  457.     ProcessInput(12, (char)0);    // R. Style Arg
  458.     ProcessInput(14, (char)48);    // Pitch Magnet
  459.     ChangeActiveLayer(2);    // 3rd Layer...
  460.     ProcessInput(7, (char)94);    // Consonance
  461.     ProcessInput(8, (char)54);    // Direction
  462.     // 127:GunShot 117:TaikoDrum
  463.     ProcessInput(9, (char)m_midi.iGetDefaultInst(2, this));    // Instrument
  464.     ProcessInput(10, (char)1);    // Active
  465.     ProcessInput(11, (char)3);    // R. Style
  466.     ProcessInput(12, (char)0);    // R. Style Arg
  467.     ProcessInput(14, (char)55);    // Pitch Magnet
  468.     /*if (m_sliLayer.GetRangeMax() <= 2)
  469.         m_sliLayer.SetRangeMax(3, TRUE);    // Update slider
  470.     else*/ for (int C=3; C<(int)CGenPoint.pvGetCoord(DIM_NUM_LAYERS); C++) {
  471.         ChangeActiveLayer(C);
  472.         ProcessInput(10, (char)0);    // Inactive
  473.     }
  474.     m_bClearDots = true; repaint();
  475. }
  476.  
  477. void OnButtonViolet() 
  478. {    showStatus("Violet (cyu@mit.edu v5.0)");
  479.     m_iBaseRed=160;m_iBaseGreen=120;m_iBaseBlue=160;
  480.     m_iMMUDRed=160;m_iMMUDGreen=120;m_iMMUDBlue=160;
  481.     m_x = m_rbMain.m_iXPos;
  482.     m_y = m_rbMain.m_iYPos + m_rbMain.m_iHeight;
  483.     m_fMM = 0; m_fUD = 1;
  484.     //m_sliMM.SetPos(0);
  485.     ProcessInput(0, (char)20);    // Cohesion
  486.     ProcessInput(1, (char)59);    // Key
  487.     ProcessInput(2, (char)79);    // Scale
  488.     ProcessInput(3, (char)31);    // Tempo
  489.     ProcessInput(4, (char)10);    // R. Consistency
  490.     ProcessInput(5, (char)80);    // R. Density
  491.     ProcessInput(6, (char)20);    // R. Length
  492.     ChangeActiveLayer(0);    // 1st Layer...
  493.     ProcessInput(7, (char)44);    // Consonance
  494.     ProcessInput(8, (char)60);    // Direction
  495.     // 30.DistGuit 39:SynthBass2
  496.     ProcessInput(9, (char)m_midi.iGetDefaultInst(6, this));    // Instrument
  497.     ProcessInput(10, (char)1);    // Active
  498.     ProcessInput(11, (char)3);    // R. Style
  499.     ProcessInput(12, (char)0);    // R. Style Arg
  500.     ProcessInput(14, (char)48);    // Pitch Magnet
  501.     ChangeActiveLayer(1);    // 1nd Layer...
  502.     ProcessInput(7, (char)64);    // Consonance
  503.     ProcessInput(8, (char)30);    // Direction
  504.     // 114:SteelDrum 100.Bright
  505.     ProcessInput(9, (char)m_midi.iGetDefaultInst(7, this));    // Instrument
  506.     ProcessInput(10, (char)1);    // Active
  507.     ProcessInput(11, (char)2);    // R. Style
  508.     ProcessInput(12, (char)42);    // R. Style Arg
  509.     ProcessInput(14, (char)64);    // Pitch Magnet
  510.     ChangeActiveLayer(2);    // 3rd Layer...
  511.     ProcessInput(7, (char)64);    // Consonance
  512.     ProcessInput(8, (char)44);    // Direction
  513.     // 124:TeleRing 115:Woodblock 118:SynthDrum
  514.     ProcessInput(9, (char)m_midi.iGetDefaultInst(8, this));    // Instrument
  515.     ProcessInput(10, (char)1);    // Active
  516.     ProcessInput(11, (char)0);    // R. Style
  517.     ProcessInput(12, (char)0);    // R. Style Arg
  518.     ProcessInput(14, (char)72);    // Pitch Magnet
  519.     /*if (m_sliLayer.GetRangeMax() <= 2)
  520.         m_sliLayer.SetRangeMax(3, TRUE);    // Update slider
  521.     else*/ for (int C=3; C<(int)CGenPoint.pvGetCoord(DIM_NUM_LAYERS); C++) {
  522.         ChangeActiveLayer(C);
  523.         ProcessInput(10, (char)0);    // Inactive
  524.     }
  525.     m_bClearDots = true; repaint();
  526. }
  527.  
  528. int i24Scale (double f01, double f10, int i00, int i01, int i10, int i11)
  529. {
  530.     int iRange0001 = i01 - i00;
  531.     int iRange1011 = i11 - i10;
  532.     double fNewRange = (double)(iRange0001) +
  533.         f10*(double)(iRange1011 - iRange0001);
  534.     double fNewStart = (double)(i00) + f10*(double)(i10 - i00);
  535.     return (int)(fNewStart + f01*fNewRange);
  536. }
  537.  
  538. void OnMMUD (double fMM, double fUD)
  539. {    showStatus ("MM " + fMM + ", UD " + fUD);
  540.     ProcessInput(0, (char)i24Scale(fMM,fUD, 80,64,20,30));    // Cohesion
  541.     ProcessInput(2, (char)(int)(fUD*79));    // Scale
  542.     ProcessInput(3, (char)i24Scale(fMM,fUD, 23,81,31,105));    // Tempo
  543.     ProcessInput(4, (char)i24Scale(fMM,fUD, 90,50,10,10));    // R. Consistency
  544.     ProcessInput(5, (char)i24Scale(fMM,fUD, 64,8,80,17));    // R. Density
  545.     ProcessInput(6, (char)i24Scale(fMM,fUD, 15,25,20,50));    // R. Length
  546.     ChangeActiveLayer(-1);
  547.     m_iMMUDRed = i24Scale(fMM,fUD, 255, 120, 160, 120);
  548.     m_iMMUDGreen = i24Scale(fMM,fUD, 120, 255, 120, 120);
  549.     m_iMMUDBlue = i24Scale(fMM,fUD, 120, 120, 160, 255);
  550.     CGenPoint.g_secTest.m_bMutateUpdate = true; repaint();
  551.     //ProcessInput(7, i24Scale(fMM,fUD, 64,64,44,74));    // Consonance
  552.     //UpdateSliderVals();
  553. }
  554.  
  555. //=============================================
  556.  
  557. public boolean mouseDown (java.awt.Event evt, int x, int y) 
  558. {    if (m_rbMain.bMouseOver(x,y)) {
  559.         m_x = x; m_y = y;
  560.         m_fMM = (double)(x-m_rbMain.m_iXPos)/(double)(m_rbMain.m_iWidth);
  561.         m_fUD = (double)(y-m_rbMain.m_iYPos)/(double)m_rbMain.m_iHeight;
  562.         OnMMUD(m_fMM, m_fUD);
  563.     }
  564.     else if (m_rbBlue.bMouseOver(x,y)) OnButtonBlue();
  565.     else if (m_rbGreen.bMouseOver(x,y)) OnButtonGreen();
  566.     else if (m_rbRed.bMouseOver(x,y)) OnButtonRed();
  567.     else if (m_rbViolet.bMouseOver(x,y)) OnButtonViolet();
  568.     else for (int C=0; C<g_iNumParams; C++)
  569.         if (marr_rsParams[C].bMouseOver(x,y)) {
  570.             CGenPoint.g_secTest.m_bMutateUpdate = true; repaint();
  571.             int iVal = (int)(marr_rsParams[C].m_fValue*127);
  572.             switch (C) {
  573.             case 10: iVal = iVal>64 ? 1 : 0; break;
  574.             case 11: iVal /= 33; break;
  575.             case 15: iVal = (int)(.5 + marr_rsParams[C].m_fValue *
  576.                 (CGenPoint.g_secTest.pstzGetPrevStanza(1).iNumLayers()));
  577.             }
  578.             ProcessInput(C, (char)iVal);
  579.             break;
  580.         }
  581.     return true;
  582. }
  583.  
  584. //-----------------------
  585. /*public boolean mouseDrag (java.awt.Event evt, int x, int y)
  586. {
  587.     if (m_rbMain.bMouseOver(x,y)) {
  588.         m_x = x; m_y = y;
  589.         m_fMM = (double)(x-m_rbMain.m_iXPos)/(double)(m_rbMain.m_iWidth);
  590.         m_fUD = (double)(y-m_rbMain.m_iYPos)/(double)m_rbMain.m_iHeight;
  591.         if (rand()%20 == 0) // Usually, don't update music.
  592.             mouseDown(evt, x, y);
  593.     }
  594.     return true;
  595. }
  596. */
  597. //-----------------------
  598. public void update (Graphics g)
  599. {    paintDots(offscreenGraphics);
  600.     if (m_bClearDots || CGenPoint.g_secTest.m_bMutateUpdate)
  601.         paint(offscreenGraphics);
  602.     g.drawImage(offscreenImage, 0, 0, this);
  603. }
  604. public void paint (Graphics g)
  605. {    //Color cMain = new Color((m_iBaseRed+m_iMMUDRed)/2,
  606.     //    (m_iBaseGreen+m_iMMUDGreen)/2, (m_iBaseBlue+m_iMMUDBlue)/2);
  607.     Color cBorder = new Color(m_iBaseRed,m_iBaseGreen,m_iBaseBlue);
  608.     if (m_bClearDots) {
  609.         //m_rbMain.paint(g, Color.black, Color.black);
  610.         g.setColor(Color.black);
  611.         g.fillRect(0,0,this.size().width,this.size().height);
  612.     }
  613.     m_rbBlue.paint(g, null, cBorder);
  614.     m_rbGreen.paint(g, null, cBorder);
  615.     m_rbRed.paint(g, null, cBorder);
  616.     m_rbViolet.paint(g, null, cBorder);
  617.     try {
  618.         if (m_bClearDots || CGenPoint.g_secTest.m_bMutateUpdate) {
  619.             for (int C=0; C<g_iNumParams; C++) {
  620.                 switch (C) {
  621.                 case 10: marr_rsParams[C].m_fValue = pvGetLastInput(C); break;
  622.                 case 11: marr_rsParams[C].m_fValue = (double)(pvGetLastInput(C))/3.0; break;
  623.                 case 15: marr_rsParams[C].m_fValue = (double)(m_iLayerNumber+1) /
  624.                     (1+CGenPoint.g_secTest.pstzGetPrevStanza(1).iNumLayers()); break;
  625.                 default: marr_rsParams[C].m_fValue = (double)(pvGetLastInput(C))/127.0; break;
  626.                 }
  627.                 marr_rsParams[C].paint(g, cBorder, Color.black);
  628.                 //if (C==3) m_x += (marr_rsParams[C].m_fValue - .5)*10;
  629.                 //if (C==2) m_y += (marr_rsParams[C].m_fValue - .5)*10;
  630.             }
  631.         } else for (int C=0; C<g_iNumParams; C++) {
  632.             marr_rsParams[C].paint(g, cBorder, Color.black);
  633.         }
  634.     } catch(Exception e);
  635.     m_bClearDots = false;
  636.     CGenPoint.g_secTest.m_bMutateUpdate = false;
  637. }
  638.  
  639. public int rand() { return (int)(Math.random() * 9747); }
  640. public void paintDots (Graphics g)
  641. {    boolean bBlack = rand()%4 == 0;
  642.     int iArmArc = 628 / ((m_x+m_y) % 8 + 1);
  643.     int iOffset = (m_x + m_y)%iArmArc;
  644.     for (int C=0; C<25; C++) {
  645.         int iDist = rand() % (1 + rand() % (m_rbMain.m_iWidth/4));
  646.         float fRadians = (float)(rand() % 628);
  647.         iDist = (int)((float)(iDist) * (float)((int)(fRadians+iOffset) % iArmArc)/(float)(iArmArc));
  648.         fRadians /= (float)(100);
  649.         int x = (int)(Math.sin(fRadians)*iDist + m_x);
  650.         int y = (int)(Math.cos(fRadians)*iDist + m_y);
  651.         if (bBlack) {
  652.             g.setColor(Color.black);
  653.             g.drawLine(x,y, x+1,y);
  654.             continue;
  655.         }
  656.         int r = i24Scale(m_fMM,m_fUD, 255, 120, 160, 120);
  657.         int gn = i24Scale(m_fMM,m_fUD, 120, 255, 120, 120);
  658.         int b = i24Scale(m_fMM,m_fUD, 120, 120, 160, 255);
  659.         //COLORREF cr = dcBmp.GetPixel(x,y);
  660.         Color cr = new Color((r+rand()%256)/2, (gn+rand()%256)/2, (b+rand()%256)/2);
  661.         g.setColor(cr);
  662.         g.drawLine(x,y, x+1,y);
  663.     }
  664. }
  665.  
  666. }    // end class Sharle
  667.