import java.util.Hashtable; import java.util.Enumeration; import java.util.BitSet; import java.util.Date; import java.io.*; import java.awt.*; import java.net.*; import java.applet.Applet; import java.lang.Math; // sin, cos // Next time: extend everything off a static global class public class Sharle extends Applet implements Runnable { Thread m_thread = null; int m_iLayerNumber = -1; int m_iMMUDRed=128, m_iMMUDGreen=128, m_iMMUDBlue=128; int m_iBaseRed=128, m_iBaseGreen=128, m_iBaseBlue=128; int m_x=0, m_y=0; double m_fMM=0, m_fUD=0; boolean m_bClearDots=false; Image offscreenImage; Graphics offscreenGraphics; RectButton m_rbMain, m_rbBlue, m_rbGreen, m_rbRed, m_rbViolet; final static int g_iNumParams=16; RectSlider marr_rsParams[]; CMIDI m_midi; // Constants int SECOND = 1000; int BEATS_PER_SEC = 24; int BEAT_TIME = (SECOND/BEATS_PER_SEC); static int g_iMinWidth = 40; static double g_fBorderWidth = .15, g_fBorderHeight = .15; // With respect to applet static double g_f4ColorDim = .25; // With respect to border static double g_fSliderWidth = .15; // With respect to distance b/w sliders static int g_i4ColorWidth = 20, g_iWidthSliders = 10; static int g_iMinHeight = 10; static int g_iWindowWidth; static int g_iWindowHeight; static int g_iMainWidth; // Globals int g_iLastTime = -1; int sm_iMIDIvolume = 64; final int BST_NO_NOTE=0; // No note on this beat final int BST_WHISPER=1; final int BST_LIGHT=2; // Faint note final int BST_MEDIUM=3; // Normal note final int BST_HEAVY=4; // Emphsized note final int BST_BANG=5; // constants final static int DIM_COHESION=0; final static int DIM_KEY=1; final static int DIM_NUM_LAYERS=2; final static int DIM_RTHM_CONSISTENCY=3; final static int DIM_RTHM_DENSITY=4; final static int DIM_RTHM_LENGTH=5; final static int DIM_SCALE=6; final static int DIM_SECTION_CHANGE=7; final static int DIM_TEMPO=8; // More constants final static int LDIM_ACTIVE=0; final static int LDIM_CONSONANCE=1; final static int LDIM_DIRECTION=2; final static int LDIM_INSTRUMENT=3; final static int LDIM_PITCH_MAGNET=4; final static int LDIM_RTHM_CONSISTENCY=5; final static int LDIM_RTHM_DENSITY=6; final static int LDIM_RTHM_LENGTH=7; final static int LDIM_RTHM_STYLE=8; final static int LDIM_RTHM_STYLE_ARG=9; final static int LDIM_TEMPO=10; void assert (boolean b) { if (!b) (new int[1])[-1] = 0; } //--------------------------------------------- public int iGetParameter (String str) { return (new Integer(getParameter(str))).intValue(); } //--------------------------------------------- public void initGUI () { //resize(g_iWindowWidth, g_iWindowHeight); int iWidth = this.size().width; int iHeight = this.size().height; m_x = iWidth/2; m_y = iHeight/2; m_fMM=0.5; m_fUD=0.5; offscreenImage = createImage(iWidth,iHeight); offscreenGraphics = offscreenImage.getGraphics(); if (iWidth > g_iMinWidth) g_iWindowWidth = iWidth; else g_iWindowWidth = g_iMinWidth; if (iHeight > g_iMinHeight) g_iWindowHeight = iHeight; else g_iWindowHeight = g_iMinHeight; int iBorderWidth = (int)( (double)(g_iWindowWidth) * g_fBorderWidth + .5); int iBorderHeight = (int)( (double)(g_iWindowHeight) * g_fBorderHeight + .5); m_rbMain = new RectButton(iBorderWidth, iBorderHeight, g_iWindowWidth - 2*iBorderWidth, g_iWindowHeight - 2*iBorderHeight); m_rbMain.m_iBorderWidth=2; int i4ColorWidth = (int)( (double)(iBorderWidth)*g_f4ColorDim + .5); int i4ColorHeight = (int)( (double)(iBorderHeight)*g_f4ColorDim + .5); m_rbBlue = new RectButton(m_rbMain.m_iXPos + m_rbMain.m_iWidth, m_rbMain.m_iYPos + m_rbMain.m_iHeight, i4ColorWidth, i4ColorHeight); m_rbBlue.m_cMain = new Color(120,120,255); m_rbGreen=new RectButton(m_rbMain.m_iXPos + m_rbMain.m_iWidth, m_rbMain.m_iYPos - i4ColorHeight, i4ColorWidth, i4ColorHeight); m_rbGreen.m_cMain = new Color(120,255,120); m_rbRed = new RectButton(m_rbMain.m_iXPos - i4ColorWidth, m_rbMain.m_iYPos - i4ColorHeight, i4ColorWidth, i4ColorHeight); m_rbRed.m_cMain = new Color(255,120,120); m_rbViolet=new RectButton(m_rbMain.m_iXPos - i4ColorWidth, m_rbMain.m_iYPos + m_rbMain.m_iHeight, i4ColorWidth, i4ColorHeight); m_rbViolet.m_cMain = new Color(160,120,160); marr_rsParams = new RectSlider[g_iNumParams]; int iEnd = 0; for (int iSide=0; iSide<4; iSide++) { int iStart = iEnd; iEnd = (iSide+1)*(g_iNumParams/4) + ((iSide= pstzCurrStanza.iNumLayers()) { m_iLayerNumber = pstzCurrStanza.iNumLayers(); CGenPoint.g_secTest.AppendLayerClone(pstzCurrStanza.plyrGetLayer(0)); } else m_iLayerNumber = iLayerIndex; System.out.println("Current layer " + m_iLayerNumber); } //----------------------- public void LoopThis (int fTime) { //showStatus("Loop " + (int)fTime); char pvNoteNum, pvVolume, pvChannel; int iNewTime = (int)(fTime)/BEAT_TIME; if (g_iLastTime != -1 && iNewTime <= g_iLastTime) return; g_iLastTime = iNewTime; CNote pNote; // Which notes to stop playing? while ((pNote = CGenPoint.g_secTest.pNoteToEnd(g_iLastTime)) != null) { pvChannel = (char)m_midi.iGetInstrumentChannel(pNote.pvInstrument); //tw.spr("off(%d,%d,%d,%d)\t", pvChannel, pNote.iNoteNum, // pNote.iBeatNum, pNote.iDuration); m_midi.MidiOutNoteOFF(pNote.iNoteNum, pvChannel); } boolean bPlayedNote = false; // Which notes to start playing? while ((pNote = CGenPoint.g_secTest.pNoteToPlay(g_iLastTime)) != null) { pvNoteNum = (char)pNote.iNoteNum; switch (pNote.bstStress) { case BST_WHISPER: pvVolume = (char)(sm_iMIDIvolume/4); break; case BST_LIGHT: pvVolume = (char)(sm_iMIDIvolume*3/4); break; case BST_MEDIUM: pvVolume = (char)(sm_iMIDIvolume); break; case BST_HEAVY: pvVolume = (char)((sm_iMIDIvolume*3 + 127)/4); break; case BST_BANG: pvVolume = (char)((sm_iMIDIvolume + 127*3)/4); break; default: pvVolume=0; System.out.println("Eh?"); break; } pvChannel = (char)m_midi.iGetInstrumentChannel(pNote.pvInstrument); //tw.spr("note(%d,%d,%d,%d,%d)\r", pvChannel, // pvNoteNum, pvVolume, pNote.iBeatNum, pNote.iDuration); //showStatus(g_iLastTime + " On " + (int)pvNoteNum); m_midi.MidiOutNoteON(pvNoteNum, pvVolume, pvChannel, fTime); bPlayedNote = true; } if (bPlayedNote || g_iLastTime % 12 == 0) { repaint(); } m_midi.LoopThis(fTime); try { Thread.sleep(25); } catch(Exception e) {} } //===================================== char pvGetLastInput (int iParamNum) { int iLyr = (m_iLayerNumber < 0) ? 0 : m_iLayerNumber; switch (iParamNum) { case 15: // no break; case -1: return (char)m_iLayerNumber; case 0: return CGenPoint.pvGetCoord(DIM_COHESION); case 1: return CGenPoint.pvGetCoord(DIM_KEY); case 2: return CGenPoint.pvGetCoord(DIM_SCALE); case 3: return CGenPoint.pvGetCoord(DIM_TEMPO); case 4: return CGenPoint.pvGetLayerCoord(LDIM_RTHM_CONSISTENCY, 0); case 5: return CGenPoint.pvGetLayerCoord(LDIM_RTHM_DENSITY, 0); case 6: return CGenPoint.pvGetLayerCoord(LDIM_RTHM_LENGTH, 0); case 7: return CGenPoint.pvGetLayerCoord(LDIM_CONSONANCE, iLyr); case 8: return CGenPoint.pvGetLayerCoord(LDIM_DIRECTION, iLyr); case 9: return CGenPoint.pvGetLayerCoord(LDIM_INSTRUMENT, iLyr); case 10: return CGenPoint.pvGetLayerCoord(LDIM_ACTIVE, iLyr); case 11: return CGenPoint.pvGetLayerCoord(LDIM_RTHM_STYLE, iLyr); case 12: return CGenPoint.pvGetLayerCoord(LDIM_RTHM_STYLE_ARG, iLyr); case 13: // no break; case 17: return CGenPoint.pvGetCoord(DIM_SECTION_CHANGE); case 14: return CGenPoint.pvGetLayerCoord(LDIM_PITCH_MAGNET, iLyr); default: assert(false); } return 0; } void ProcessInput (int iParamNum, char pvInput) {System.out.println("Processing " + iParamNum + "," + (int)pvInput); switch (iParamNum) { case 0: CGenPoint.iSetCoord(DIM_COHESION, pvInput); return; case 1: CGenPoint.iSetCoord(DIM_KEY, pvInput); return; case 2: CGenPoint.iSetCoord(DIM_SCALE, pvInput); return; case 3: CGenPoint.iSetCoord(DIM_TEMPO, pvInput); return; case 4: CGenPoint.iSetCoord(DIM_RTHM_CONSISTENCY, pvInput); return; case 5: CGenPoint.iSetCoord(DIM_RTHM_DENSITY, pvInput); return; case 6: CGenPoint.iSetCoord(DIM_RTHM_LENGTH, pvInput); return; case 13: // no break; case 17: CGenPoint.iSetCoord(DIM_SECTION_CHANGE, pvInput); return; case 15: ChangeActiveLayer(pvInput); return; } int iStart, iEnd; if (m_iLayerNumber >= 0) { iStart = m_iLayerNumber; iEnd = m_iLayerNumber; if (iParamNum == 10 && pvInput == 0 && iStart==0) iStart++; // First layer always active } else { iStart = 0; iEnd = CGenPoint.g_secTest.pstzGetPrevStanza(1).iNumLayers() - 1; if (iParamNum == 10 && pvInput == 0) iStart++; // First layer always active } for (int C=iStart; C<=iEnd; C++) { switch (iParamNum) { case 7: CGenPoint.iSetLayerCoord(LDIM_CONSONANCE, C, pvInput); break; case 8: CGenPoint.iSetLayerCoord(LDIM_DIRECTION, C, pvInput); break; case 9: CGenPoint.iSetLayerCoord(LDIM_INSTRUMENT, C, pvInput); break; case 10: CGenPoint.iSetLayerCoord(LDIM_ACTIVE, C, pvInput); break; case 11: CGenPoint.iSetLayerCoord(LDIM_RTHM_STYLE, C, pvInput); CGenPoint.iSetLayerCoord(LDIM_RTHM_STYLE_ARG, C, (char)64); break; case 12: CGenPoint.iSetLayerCoord(LDIM_RTHM_STYLE_ARG, C, pvInput); break; case 14: CGenPoint.iSetLayerCoord(LDIM_PITCH_MAGNET, C, pvInput); break; default: assert(false); } } } //================================================= void OnButtonBlue() { showStatus("Blue"); m_iBaseRed=120;m_iBaseGreen=120;m_iBaseBlue=255; m_iMMUDRed=120;m_iMMUDGreen=120;m_iMMUDBlue=255; m_x = m_rbMain.m_iXPos + m_rbMain.m_iWidth; m_y = m_rbMain.m_iYPos + m_rbMain.m_iHeight; m_fMM = 1; m_fUD = 1; ProcessInput(0, (char)30); // Cohesion ProcessInput(1, (char)61); // Key ProcessInput(2, (char)50); // Scale ProcessInput(3, (char)105); // Tempo ProcessInput(4, (char)10); // R. Consistency ProcessInput(5, (char)17); // R. Density ProcessInput(6, (char)50); // R. Length ChangeActiveLayer(0); ProcessInput(7, (char)74); // Consonance ProcessInput(8, (char)50); // Direction // 20.ReedOrgan 53:Oohs 72:Piccolo 49.StrEns2 ProcessInput(9, (char)m_midi.iGetDefaultInst(5, this)); // Instrument ProcessInput(10, (char)1); // Active ProcessInput(11, (char)3); // R. Style ProcessInput(12, (char)0); // R. Style Arg ProcessInput(14, (char)43); // Pitch Magnet for (int C=1; C<(int)CGenPoint.pvGetCoord(DIM_NUM_LAYERS); C++) { ChangeActiveLayer(C); ProcessInput(10, (char)0); // Inactive } m_bClearDots = true; repaint(); } void OnButtonGreen() { showStatus("Green"); m_iBaseRed=120;m_iBaseGreen=255;m_iBaseBlue=120; m_iMMUDRed=120;m_iMMUDGreen=255;m_iMMUDBlue=120; m_x = m_rbMain.m_iXPos + m_rbMain.m_iWidth; m_y = m_rbMain.m_iYPos; m_fMM = 1; m_fUD = 0; ProcessInput(0, (char)64); // Cohesion ProcessInput(1, (char)72); // Key ProcessInput(2, (char)10); // Scale ProcessInput(3, (char)81); // Tempo ProcessInput(4, (char)50); // R. Consistency ProcessInput(5, (char)8); // R. Density ProcessInput(6, (char)25); // R. Length ChangeActiveLayer(0); // 1st Layer... ProcessInput(7, (char)64); // Consonance ProcessInput(8, (char)54); // Direction // 50:SynthStr1 97:Soundtrack ProcessInput(9, (char)m_midi.iGetDefaultInst(3, this)); // Instrument ProcessInput(10, (char)1); // Active ProcessInput(11, (char)3); // R. Style ProcessInput(12, (char)0); // R. Style Arg ProcessInput(14, (char)65); // Pitch Magnet ChangeActiveLayer(1); // 2nd Layer... ProcessInput(7, (char)94); // Consonance ProcessInput(8, (char)32); // Direction // 94:Halo 102:Echoes ProcessInput(9, (char)m_midi.iGetDefaultInst(4, this)); // Instrument ProcessInput(10, (char)1); // Active ProcessInput(11, (char)1); // R. Style ProcessInput(12, (char)64); // R. Style Arg ProcessInput(14, (char)84); // Pitch Magnet /*if (m_sliLayer.GetRangeMax() <= 1) m_sliLayer.SetRangeMax(2, TRUE); // Update slider else*/ for (int C=2; C<(int)CGenPoint.pvGetCoord(DIM_NUM_LAYERS); C++) { ChangeActiveLayer(C); ProcessInput(10, (char)0); // Inactive } m_bClearDots = true; repaint(); } void OnButtonRed() { showStatus("Red"); m_iBaseRed=255;m_iBaseGreen=120;m_iBaseBlue=120; m_iMMUDRed=255;m_iMMUDGreen=120;m_iMMUDBlue=120; m_x = m_rbMain.m_iXPos; m_y = m_rbMain.m_iYPos; m_fMM = 0; m_fUD = 0; ProcessInput(0, (char)80); // Cohesion ProcessInput(1, (char)60); // Key ProcessInput(2, (char)30); // Scale ProcessInput(3, (char)23); // Tempo ProcessInput(4, (char)90); // R. Consistency ProcessInput(5, (char)64); // R. Density ProcessInput(6, (char)15); // R. Length ChangeActiveLayer(0); // 1st Layer... ProcessInput(7, (char)54); // Consonance ProcessInput(8, (char)54); // Direction // 96:Rain 25:GuitSteel ProcessInput(9, (char)m_midi.iGetDefaultInst(0, this)); // Instrument ProcessInput(10, (char)1); // Active ProcessInput(11, (char)3); // R. Style ProcessInput(12, (char)0); // R. Style Arg ProcessInput(14, (char)72); // Pitch Magnet ChangeActiveLayer(1); // 2nd Layer... ProcessInput(7, (char)94); // Consonance ProcessInput(8, (char)64); // Direction // 88:NewAge ProcessInput(9, (char)m_midi.iGetDefaultInst(1, this)); // Instrument ProcessInput(10, (char)1); // Active ProcessInput(11, (char)0); // R. Style ProcessInput(12, (char)0); // R. Style Arg ProcessInput(14, (char)48); // Pitch Magnet ChangeActiveLayer(2); // 3rd Layer... ProcessInput(7, (char)94); // Consonance ProcessInput(8, (char)54); // Direction // 127:GunShot 117:TaikoDrum ProcessInput(9, (char)m_midi.iGetDefaultInst(2, this)); // Instrument ProcessInput(10, (char)1); // Active ProcessInput(11, (char)3); // R. Style ProcessInput(12, (char)0); // R. Style Arg ProcessInput(14, (char)55); // Pitch Magnet /*if (m_sliLayer.GetRangeMax() <= 2) m_sliLayer.SetRangeMax(3, TRUE); // Update slider else*/ for (int C=3; C<(int)CGenPoint.pvGetCoord(DIM_NUM_LAYERS); C++) { ChangeActiveLayer(C); ProcessInput(10, (char)0); // Inactive } m_bClearDots = true; repaint(); } void OnButtonViolet() { showStatus("Violet (cyu@mit.edu v5.0)"); m_iBaseRed=160;m_iBaseGreen=120;m_iBaseBlue=160; m_iMMUDRed=160;m_iMMUDGreen=120;m_iMMUDBlue=160; m_x = m_rbMain.m_iXPos; m_y = m_rbMain.m_iYPos + m_rbMain.m_iHeight; m_fMM = 0; m_fUD = 1; //m_sliMM.SetPos(0); ProcessInput(0, (char)20); // Cohesion ProcessInput(1, (char)59); // Key ProcessInput(2, (char)79); // Scale ProcessInput(3, (char)31); // Tempo ProcessInput(4, (char)10); // R. Consistency ProcessInput(5, (char)80); // R. Density ProcessInput(6, (char)20); // R. Length ChangeActiveLayer(0); // 1st Layer... ProcessInput(7, (char)44); // Consonance ProcessInput(8, (char)60); // Direction // 30.DistGuit 39:SynthBass2 ProcessInput(9, (char)m_midi.iGetDefaultInst(6, this)); // Instrument ProcessInput(10, (char)1); // Active ProcessInput(11, (char)3); // R. Style ProcessInput(12, (char)0); // R. Style Arg ProcessInput(14, (char)48); // Pitch Magnet ChangeActiveLayer(1); // 1nd Layer... ProcessInput(7, (char)64); // Consonance ProcessInput(8, (char)30); // Direction // 114:SteelDrum 100.Bright ProcessInput(9, (char)m_midi.iGetDefaultInst(7, this)); // Instrument ProcessInput(10, (char)1); // Active ProcessInput(11, (char)2); // R. Style ProcessInput(12, (char)42); // R. Style Arg ProcessInput(14, (char)64); // Pitch Magnet ChangeActiveLayer(2); // 3rd Layer... ProcessInput(7, (char)64); // Consonance ProcessInput(8, (char)44); // Direction // 124:TeleRing 115:Woodblock 118:SynthDrum ProcessInput(9, (char)m_midi.iGetDefaultInst(8, this)); // Instrument ProcessInput(10, (char)1); // Active ProcessInput(11, (char)0); // R. Style ProcessInput(12, (char)0); // R. Style Arg ProcessInput(14, (char)72); // Pitch Magnet /*if (m_sliLayer.GetRangeMax() <= 2) m_sliLayer.SetRangeMax(3, TRUE); // Update slider else*/ for (int C=3; C<(int)CGenPoint.pvGetCoord(DIM_NUM_LAYERS); C++) { ChangeActiveLayer(C); ProcessInput(10, (char)0); // Inactive } m_bClearDots = true; repaint(); } int i24Scale (double f01, double f10, int i00, int i01, int i10, int i11) { int iRange0001 = i01 - i00; int iRange1011 = i11 - i10; double fNewRange = (double)(iRange0001) + f10*(double)(iRange1011 - iRange0001); double fNewStart = (double)(i00) + f10*(double)(i10 - i00); return (int)(fNewStart + f01*fNewRange); } void OnMMUD (double fMM, double fUD) { showStatus ("MM " + fMM + ", UD " + fUD); ProcessInput(0, (char)i24Scale(fMM,fUD, 80,64,20,30)); // Cohesion ProcessInput(2, (char)(int)(fUD*79)); // Scale ProcessInput(3, (char)i24Scale(fMM,fUD, 23,81,31,105)); // Tempo ProcessInput(4, (char)i24Scale(fMM,fUD, 90,50,10,10)); // R. Consistency ProcessInput(5, (char)i24Scale(fMM,fUD, 64,8,80,17)); // R. Density ProcessInput(6, (char)i24Scale(fMM,fUD, 15,25,20,50)); // R. Length ChangeActiveLayer(-1); m_iMMUDRed = i24Scale(fMM,fUD, 255, 120, 160, 120); m_iMMUDGreen = i24Scale(fMM,fUD, 120, 255, 120, 120); m_iMMUDBlue = i24Scale(fMM,fUD, 120, 120, 160, 255); CGenPoint.g_secTest.m_bMutateUpdate = true; repaint(); //ProcessInput(7, i24Scale(fMM,fUD, 64,64,44,74)); // Consonance //UpdateSliderVals(); } //============================================= public boolean mouseDown (java.awt.Event evt, int x, int y) { if (m_rbMain.bMouseOver(x,y)) { m_x = x; m_y = y; m_fMM = (double)(x-m_rbMain.m_iXPos)/(double)(m_rbMain.m_iWidth); m_fUD = (double)(y-m_rbMain.m_iYPos)/(double)m_rbMain.m_iHeight; OnMMUD(m_fMM, m_fUD); } else if (m_rbBlue.bMouseOver(x,y)) OnButtonBlue(); else if (m_rbGreen.bMouseOver(x,y)) OnButtonGreen(); else if (m_rbRed.bMouseOver(x,y)) OnButtonRed(); else if (m_rbViolet.bMouseOver(x,y)) OnButtonViolet(); else for (int C=0; C64 ? 1 : 0; break; case 11: iVal /= 33; break; case 15: iVal = (int)(.5 + marr_rsParams[C].m_fValue * (CGenPoint.g_secTest.pstzGetPrevStanza(1).iNumLayers())); } ProcessInput(C, (char)iVal); break; } return true; } //----------------------- /*public boolean mouseDrag (java.awt.Event evt, int x, int y) { if (m_rbMain.bMouseOver(x,y)) { m_x = x; m_y = y; m_fMM = (double)(x-m_rbMain.m_iXPos)/(double)(m_rbMain.m_iWidth); m_fUD = (double)(y-m_rbMain.m_iYPos)/(double)m_rbMain.m_iHeight; if (rand()%20 == 0) // Usually, don't update music. mouseDown(evt, x, y); } return true; } */ //----------------------- public void update (Graphics g) { paintDots(offscreenGraphics); if (m_bClearDots || CGenPoint.g_secTest.m_bMutateUpdate) paint(offscreenGraphics); g.drawImage(offscreenImage, 0, 0, this); } public void paint (Graphics g) { //Color cMain = new Color((m_iBaseRed+m_iMMUDRed)/2, // (m_iBaseGreen+m_iMMUDGreen)/2, (m_iBaseBlue+m_iMMUDBlue)/2); Color cBorder = new Color(m_iBaseRed,m_iBaseGreen,m_iBaseBlue); if (m_bClearDots) { //m_rbMain.paint(g, Color.black, Color.black); g.setColor(Color.black); g.fillRect(0,0,this.size().width,this.size().height); } m_rbBlue.paint(g, null, cBorder); m_rbGreen.paint(g, null, cBorder); m_rbRed.paint(g, null, cBorder); m_rbViolet.paint(g, null, cBorder); try { if (m_bClearDots || CGenPoint.g_secTest.m_bMutateUpdate) { for (int C=0; C