home *** CD-ROM | disk | FTP | other *** search
Java Source | 2017-09-21 | 7.2 KB | 222 lines |
- import java.applet.Applet;
-
- public class CSection
- { // Globals
- final int MAX_ACTIVE_NOTES = 32;
- final char MID_C = 60;
- int sm_iNumActiveNotes;
- CNote sm_arrNotesActive[]; // Notes still on.
- CLineMods g_lmTestHmnyRthm;
- CLineMods g_lmTestMel;
-
- // Members
- // ActivityLevel m_alActivity;
- CScale m_sclScale;
- char m_pvKey;
- char m_pvCohesion;
- char m_pvChangeRate; // How often we reach the end of a section.
-
- CStanza m_arrpStanzas[];
- int m_iNumStanzas;
- int m_iNumStanzasAllocated;
-
- CRhythm m_arrpRthms[];
- int m_iNumRthms;
- int m_iNumRthmsAllocated;
- //======bogus global functions=====
- public int mod (int x, int y)
- { if (x>=0) return x%y;
- int iNegMod = (-x) % y;
- return (0==iNegMod) ? y-iNegMod : 0;
- }
- public void assert(boolean b) { if (!b) m_arrpStanzas[-1] = null; }
- public int rand() { return (int)(Math.random() * 9747); }
- //================================
- boolean m_bMutateUpdate = false;
- CSection (Applet pParent)
- { sm_arrNotesActive = new CNote[MAX_ACTIVE_NOTES];
- m_pvKey=MID_C; m_pvCohesion=64; m_pvChangeRate=0;
- m_arrpRthms=null; m_iNumRthms=0; m_iNumRthmsAllocated=0;
- m_arrpStanzas=null; m_iNumStanzas=0; m_iNumStanzasAllocated=0;
- g_lmTestHmnyRthm = new CLineMods(null);
- g_lmTestMel = new CLineMods(null);
- m_sclScale = new CScale("44142414424");
- }
- // Operations
- //----------------------------------
- public CNote pNoteToPlay (int iCurrBeatNum)
- {
- if (0==m_iNumStanzas || m_arrpStanzas[m_iNumStanzas-1].
- bDonePlaying(iCurrBeatNum))
- { if (0!=m_iNumStanzas)
- {
- if (m_arrpStanzas[m_iNumStanzas-1].
- pcmlGetPrevCMLine(1).bDoCadence())
- { // If we just finished playing a cadence,
- // ...play a delay (length depends on tempo):
- char pvTempo = m_arrpStanzas[m_iNumStanzas-1].
- rthmGetRhythm(0).pvGetTempo();
- // After each delay of length pvTempo, there's a 1/12
- // chance of ending the delay:
- if (0!=mod(iCurrBeatNum, (int)(pvTempo)) ||
- 0!=mod(rand(), 8)) {
- if ((int)pvTempo > 1)
- return null; // Still delayed.
- }
- // No longer delayed
- // Let's mutate after the delay:
- for (int C=0; C<6; C++) {
- CGenPoint.iRandomTweak();
- }
- m_bMutateUpdate = true;
- }
- }
- AppendNewStanza();
- if (m_iNumStanzas == 1) // If first stanza of section...
- { if (0==m_iNumRthms) AppendNewRhythm(/*"1b3b5b7b8b"*/);
- m_arrpStanzas[m_iNumStanzas-1].
- Initialize(m_arrpRthms[0], 1+pvGetCohesion()/13, 4);
- }
- // Otherwise, use previous stanza as reference:
- else m_arrpStanzas[m_iNumStanzas-1].
- Initialize2(m_arrpStanzas[m_iNumStanzas-2], 1+pvGetCohesion()/13);
- }
- return m_arrpStanzas[m_iNumStanzas-1].
- pNoteToPlay(iCurrBeatNum);
- }
- //----------------------------------
- public CNote pNoteToEnd (int iCurrBeatNum)
- {
- if (0==m_iNumStanzas)
- return null;
- return m_arrpStanzas[m_iNumStanzas-1].
- pNoteToEnd(iCurrBeatNum);
- }
-
- //----------------------------------
- public void AppendLayerClone (CLayer plyrRef)
- {
- AppendNewRhythm(/*null*/); // Append non-descript rhythm.
-
- // Fill non-descript rhythm w/ clone of that in plyrRef:
- CRhythm pCurrRthm = pGetPrevRhythm(1);
- pCurrRthm.bSet(plyrRef.pRhythm());
-
- // Append an inactive layer (with this new rhythm) to our
- // current stanza:
- CStanza pCurrStz = pstzGetPrevStanza(1);
- pCurrStz.AppendNewLayer(pCurrRthm, false);
-
- // Append a sub-line to the current line to match the new layer:
- pCurrStz.pcmlGetPrevCMLine(1).
- AppendAdditionalLine(plyrRef.pvPitchMagnet());
- }
-
- //----------------------------------
- public boolean bSetCurrentTempo (int iNewTempo, int iLayerNum/* = -1*/)
- { if (0==m_iNumStanzas) return false;
- return m_arrpStanzas[m_iNumStanzas-1].
- bSetCurrTempo(iNewTempo, iLayerNum);
- }
- //----------------------------------
- public boolean bSetCurrCenterNote (int iNewCenter)
- { if (0==m_iNumStanzas) return false;
- return m_arrpStanzas[m_iNumStanzas-1].
- bSetCurrCenterNote(iNewCenter);
- }
- //----------------------------------
- /*public boolean bSetCurrRhythm (String szNewRthm, int iLayerIndex=0)
- {
- if (0==m_iNumStanzas) return false;
- return m_arrpStanzas[m_iNumStanzas-1].
- bSetRhythm(szNewRthm, iLayerIndex);
- }*/
- //----------------------------------
- public CScale sclGetScale () { return m_sclScale; }
-
- //----------------------------------
- public void SetKey (char pvNewKey) { m_pvKey = pvNewKey; }
- //----------------------------------
- public char pvGetKey () { return m_pvKey; }
-
- //----------------------------------
- public void SetCohesion (char pvCoh)
- { m_pvCohesion = pvCoh; }
- //----------------------------------
- public char pvGetCohesion () { return m_pvCohesion; }
-
- //----------------------------------
- public boolean bSetChangeRate (char pvNewRate)
- { m_pvChangeRate = pvNewRate; return true; }
- //----------------------------------
- public char pvChangeRate () { return m_pvChangeRate; }
-
- //----------------------------------
- public CStanza pstzGetStanza (int iStanzaIndex)
- { assert(0 <= iStanzaIndex && iStanzaIndex < m_iNumStanzas);
- return m_arrpStanzas[iStanzaIndex]; }
- //----------------------------------
- public CStanza pstzGetPrevStanza (int iBackNumStanzas/*=1*/)
- { return pstzGetStanza(m_iNumStanzas - iBackNumStanzas); }
- //----------------------------------
- public int iNumStanzas () { return m_iNumStanzas; }
-
- //----------------------------------
- public CRhythm pGetRhythm (int iIndex)
- { return m_arrpRthms[iIndex]; }
- //----------------------------------
- public CRhythm pGetPrevRhythm (int iStepsBack /*= 1*/)
- { return pGetRhythm(m_iNumRthms - iStepsBack); }
- //----------------------------------
- public int iNumRhythms () { return m_iNumRthms; }
- //----------------------------------
- public void AppendNewRhythm (/*String szNewRthm = 0*/)
- { if (m_iNumRthms == m_iNumRthmsAllocated)
- { m_iNumRthmsAllocated += 3;
- CRhythm[] temp = m_arrpRthms;
- m_arrpRthms = new CRhythm[m_iNumRthmsAllocated];
- for (int C=0; C<m_iNumRthmsAllocated-3; C++)
- m_arrpRthms[C] = temp[C];
- }
- m_iNumRthms++;
- m_arrpRthms[m_iNumRthms-1] = new CRhythm();
- //if (szNewRthm)
- // if (!m_arrpRthms[m_iNumRthms-1].bSet(szNewRthm))
- // assert(0);
- m_arrpRthms[m_iNumRthms-1].bGenerate((char)40,(char)40,(char)30);
- }
- //=======================================
- // Implementation
- void PackStanzas()
- { if (m_iNumStanzas < 3) return;
- int C;
- for (C=1; C<m_iNumStanzas/2; C++)
- { //delete m_arrpStanzas[C];
- m_arrpStanzas[C] = m_arrpStanzas[C*2];
- m_arrpStanzas[C*2] = null;
- }
- //delete m_arrpStanzas[m_iNumStanzas/2];
- m_arrpStanzas[m_iNumStanzas/2] =
- m_arrpStanzas[m_iNumStanzas-1];
- m_arrpStanzas[m_iNumStanzas-1] = null;
- for (C=m_iNumStanzas/2+1; C<m_iNumStanzas; C++)
- { //delete m_arrpStanzas[C];
- m_arrpStanzas[C] = null;
- }
- m_iNumStanzas = m_iNumStanzas/2 + 1;
- }
- //------------------------
- void AppendNewStanza()
- { if (m_iNumStanzas > 7) PackStanzas();
- if (m_iNumStanzas == m_iNumStanzasAllocated)
- { m_iNumStanzasAllocated += 5;
- CStanza[] temp = m_arrpStanzas;
- m_arrpStanzas = new CStanza[m_iNumStanzasAllocated];
- for (int C=0; C<m_iNumStanzasAllocated-5; C++)
- m_arrpStanzas[C] = temp[C];
- }
- m_iNumStanzas++;
- m_arrpStanzas[m_iNumStanzas-1] = new CStanza(this);
- assert(null!=m_arrpStanzas[m_iNumStanzas-1]);
- }
- } // end class