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 / CSection.java < prev    next >
Encoding:
Java Source  |  2017-09-21  |  7.2 KB  |  222 lines

  1. import java.applet.Applet;
  2.  
  3. public class CSection
  4. {    // Globals
  5.     final int MAX_ACTIVE_NOTES = 32;
  6.     final char MID_C = 60;
  7.     int sm_iNumActiveNotes;
  8.     CNote sm_arrNotesActive[];    // Notes still on.
  9.     CLineMods g_lmTestHmnyRthm;
  10.     CLineMods g_lmTestMel;
  11.  
  12. // Members
  13.     // ActivityLevel m_alActivity;
  14.     CScale m_sclScale;
  15.     char m_pvKey;
  16.     char m_pvCohesion;
  17.     char m_pvChangeRate;    // How often we reach the end of a section.
  18.     
  19.     CStanza m_arrpStanzas[];
  20.     int m_iNumStanzas;
  21.     int m_iNumStanzasAllocated;
  22.  
  23.     CRhythm m_arrpRthms[];
  24.     int m_iNumRthms;
  25.     int m_iNumRthmsAllocated;
  26. //======bogus global functions=====
  27. public int mod (int x, int y)
  28. {    if (x>=0) return x%y;
  29.     int iNegMod = (-x) % y;
  30.     return (0==iNegMod) ? y-iNegMod : 0;
  31. }
  32. public void assert(boolean b) { if (!b) m_arrpStanzas[-1] = null; }
  33. public int rand() { return (int)(Math.random() * 9747); }
  34. //================================
  35. boolean m_bMutateUpdate = false;
  36. CSection (Applet pParent)
  37. {    sm_arrNotesActive = new CNote[MAX_ACTIVE_NOTES];
  38.     m_pvKey=MID_C; m_pvCohesion=64; m_pvChangeRate=0;
  39.     m_arrpRthms=null; m_iNumRthms=0; m_iNumRthmsAllocated=0;
  40.     m_arrpStanzas=null; m_iNumStanzas=0; m_iNumStanzasAllocated=0;
  41.     g_lmTestHmnyRthm = new CLineMods(null);
  42.     g_lmTestMel = new CLineMods(null);
  43.     m_sclScale = new CScale("44142414424");
  44. }
  45. // Operations
  46. //----------------------------------
  47. public CNote pNoteToPlay (int iCurrBeatNum)
  48. {
  49.     if (0==m_iNumStanzas || m_arrpStanzas[m_iNumStanzas-1].
  50.         bDonePlaying(iCurrBeatNum))
  51.     {    if (0!=m_iNumStanzas)
  52.         {
  53.             if (m_arrpStanzas[m_iNumStanzas-1].
  54.                 pcmlGetPrevCMLine(1).bDoCadence())
  55.             {    // If we just finished playing a cadence,
  56.                 // ...play a delay (length depends on tempo):
  57.                 char pvTempo = m_arrpStanzas[m_iNumStanzas-1].
  58.                     rthmGetRhythm(0).pvGetTempo();
  59.                 // After each delay of length pvTempo, there's a 1/12
  60.                 // chance of ending the delay:                
  61.                 if (0!=mod(iCurrBeatNum, (int)(pvTempo)) ||
  62.                     0!=mod(rand(), 8)) {
  63.                     if ((int)pvTempo > 1)
  64.                         return null;    // Still delayed.
  65.                 }
  66.                 // No longer delayed
  67.                 // Let's mutate after the delay:
  68.                 for (int C=0; C<6; C++) {
  69.                     CGenPoint.iRandomTweak();
  70.                 }
  71.                 m_bMutateUpdate = true;
  72.             }
  73.         }
  74.         AppendNewStanza();        
  75.         if (m_iNumStanzas == 1)    // If first stanza of section...
  76.         {    if (0==m_iNumRthms) AppendNewRhythm(/*"1b3b5b7b8b"*/);
  77.             m_arrpStanzas[m_iNumStanzas-1].
  78.                 Initialize(m_arrpRthms[0], 1+pvGetCohesion()/13, 4);
  79.         }
  80.         // Otherwise, use previous stanza as reference:
  81.         else m_arrpStanzas[m_iNumStanzas-1].
  82.             Initialize2(m_arrpStanzas[m_iNumStanzas-2], 1+pvGetCohesion()/13);
  83.     }
  84.     return m_arrpStanzas[m_iNumStanzas-1].
  85.         pNoteToPlay(iCurrBeatNum);
  86. }
  87. //----------------------------------
  88. public CNote pNoteToEnd (int iCurrBeatNum)
  89. {
  90.     if (0==m_iNumStanzas)
  91.         return null;
  92.     return m_arrpStanzas[m_iNumStanzas-1].
  93.         pNoteToEnd(iCurrBeatNum);
  94. }
  95.  
  96. //----------------------------------
  97. public void AppendLayerClone (CLayer plyrRef)
  98. {
  99.     AppendNewRhythm(/*null*/);    // Append non-descript rhythm.
  100.     
  101.     // Fill non-descript rhythm w/ clone of that in plyrRef:
  102.     CRhythm pCurrRthm = pGetPrevRhythm(1);
  103.     pCurrRthm.bSet(plyrRef.pRhythm());
  104.     
  105.     // Append an inactive layer (with this new rhythm) to our
  106.     // current stanza:
  107.     CStanza pCurrStz = pstzGetPrevStanza(1);
  108.     pCurrStz.AppendNewLayer(pCurrRthm, false);
  109.     
  110.     // Append a sub-line to the current line to match the new layer:
  111.     pCurrStz.pcmlGetPrevCMLine(1).
  112.         AppendAdditionalLine(plyrRef.pvPitchMagnet());
  113. }
  114.     
  115. //----------------------------------
  116. public boolean bSetCurrentTempo (int iNewTempo, int iLayerNum/* = -1*/)
  117. {    if (0==m_iNumStanzas) return false;
  118.     return m_arrpStanzas[m_iNumStanzas-1].
  119.         bSetCurrTempo(iNewTempo, iLayerNum);
  120. }
  121. //----------------------------------
  122. public boolean bSetCurrCenterNote (int iNewCenter)
  123. {    if (0==m_iNumStanzas) return false;
  124.     return m_arrpStanzas[m_iNumStanzas-1].
  125.         bSetCurrCenterNote(iNewCenter);
  126. }
  127. //----------------------------------
  128. /*public boolean bSetCurrRhythm (String szNewRthm, int iLayerIndex=0)
  129. {
  130.     if (0==m_iNumStanzas) return false;
  131.     return m_arrpStanzas[m_iNumStanzas-1].
  132.         bSetRhythm(szNewRthm, iLayerIndex);
  133. }*/
  134. //----------------------------------
  135. public CScale sclGetScale () { return m_sclScale; }
  136.     
  137. //----------------------------------
  138. public void SetKey (char pvNewKey) { m_pvKey = pvNewKey; }
  139. //----------------------------------
  140. public char pvGetKey () { return m_pvKey; }
  141.     
  142. //----------------------------------
  143. public void SetCohesion (char pvCoh)
  144.     { m_pvCohesion = pvCoh; }
  145. //----------------------------------
  146. public char pvGetCohesion () { return m_pvCohesion; }
  147.  
  148. //----------------------------------
  149. public boolean bSetChangeRate (char pvNewRate)
  150.     { m_pvChangeRate = pvNewRate; return true; }
  151. //----------------------------------
  152. public char pvChangeRate () { return m_pvChangeRate; }
  153.  
  154. //----------------------------------
  155. public CStanza pstzGetStanza (int iStanzaIndex)
  156.     {    assert(0 <= iStanzaIndex && iStanzaIndex < m_iNumStanzas);
  157.         return m_arrpStanzas[iStanzaIndex];    }
  158. //----------------------------------
  159. public CStanza pstzGetPrevStanza (int iBackNumStanzas/*=1*/)
  160.     {    return pstzGetStanza(m_iNumStanzas - iBackNumStanzas); }
  161. //----------------------------------
  162. public int iNumStanzas () { return m_iNumStanzas; }
  163.     
  164. //----------------------------------
  165. public CRhythm pGetRhythm (int iIndex)
  166.     {    return m_arrpRthms[iIndex]; }
  167. //----------------------------------
  168. public CRhythm pGetPrevRhythm (int iStepsBack /*= 1*/)
  169.     {    return pGetRhythm(m_iNumRthms - iStepsBack); }
  170. //----------------------------------
  171. public int iNumRhythms () { return m_iNumRthms; }
  172. //----------------------------------
  173. public void AppendNewRhythm (/*String szNewRthm = 0*/)
  174. {    if (m_iNumRthms == m_iNumRthmsAllocated)
  175.     {    m_iNumRthmsAllocated += 3;
  176.         CRhythm[] temp = m_arrpRthms;
  177.         m_arrpRthms = new CRhythm[m_iNumRthmsAllocated];
  178.         for (int C=0; C<m_iNumRthmsAllocated-3; C++)
  179.             m_arrpRthms[C] = temp[C];
  180.     }
  181.     m_iNumRthms++;
  182.     m_arrpRthms[m_iNumRthms-1] = new CRhythm();
  183.     //if (szNewRthm)
  184.     //    if (!m_arrpRthms[m_iNumRthms-1].bSet(szNewRthm))
  185.     //        assert(0);
  186.     m_arrpRthms[m_iNumRthms-1].bGenerate((char)40,(char)40,(char)30);
  187. }
  188. //=======================================
  189. // Implementation
  190. void PackStanzas()
  191. {    if (m_iNumStanzas < 3) return;
  192.     int C;
  193.     for (C=1; C<m_iNumStanzas/2; C++)
  194.     {    //delete m_arrpStanzas[C];
  195.         m_arrpStanzas[C] = m_arrpStanzas[C*2];
  196.         m_arrpStanzas[C*2] = null;
  197.     }
  198.     //delete m_arrpStanzas[m_iNumStanzas/2];
  199.     m_arrpStanzas[m_iNumStanzas/2] =
  200.         m_arrpStanzas[m_iNumStanzas-1];
  201.     m_arrpStanzas[m_iNumStanzas-1] = null;
  202.     for (C=m_iNumStanzas/2+1; C<m_iNumStanzas; C++)
  203.     {    //delete m_arrpStanzas[C];
  204.         m_arrpStanzas[C] = null;
  205.     }
  206.     m_iNumStanzas = m_iNumStanzas/2 + 1;
  207. }
  208. //------------------------
  209. void AppendNewStanza()
  210. {    if (m_iNumStanzas > 7) PackStanzas();
  211.     if (m_iNumStanzas == m_iNumStanzasAllocated)
  212.     {    m_iNumStanzasAllocated += 5;
  213.         CStanza[] temp = m_arrpStanzas;
  214.         m_arrpStanzas = new CStanza[m_iNumStanzasAllocated];
  215.         for (int C=0; C<m_iNumStanzasAllocated-5; C++)
  216.             m_arrpStanzas[C] = temp[C];
  217.     }
  218.     m_iNumStanzas++;
  219.     m_arrpStanzas[m_iNumStanzas-1] = new CStanza(this);
  220.     assert(null!=m_arrpStanzas[m_iNumStanzas-1]);
  221. }
  222. }    // end class