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 / CCompoundMusicLine.java < prev    next >
Encoding:
Java Source  |  2017-09-21  |  4.7 KB  |  153 lines

  1.  
  2. //========================================================
  3. // Multiple musical lines played at the same time:
  4. public class CCompoundMusicLine    // Hungarian: cml
  5. {
  6. // Members
  7.     CStanza m_pstzParent;
  8.     boolean m_bCadence;
  9.     CMusicLine m_arrpMusicLines[];
  10.     int m_iNumLines;
  11.     int m_iNumAllocatedLines;
  12.  
  13.     // Memoization:
  14.     int iMemoizedBeatNum = -1;
  15.     int iMemoizedNumLines = -1;
  16.     int bMemoizedAnswer = -1;
  17.     CCompoundMusicLine pcmlMemoized = null;
  18.  
  19. public void assert(boolean b) { if (!b) m_arrpMusicLines[-1] = null; }
  20.  
  21. // Operations
  22. //-------------------------------
  23. public CNote pNoteToPlay (int iCurrBeatNum)
  24. {    assert(m_iNumLines > 0);
  25.     CSection sec = m_pstzParent.m_psecParent;
  26.     for (int C=0; C<m_iNumLines; C++)
  27.     {    CNote pnote;
  28.         if (C==0)
  29.         {    // Melody:
  30.             pnote = m_arrpMusicLines[C].
  31.                 pNoteToPlay(iCurrBeatNum, sec.g_lmTestMel);
  32.         } else // Harmony:
  33.         {    sec.g_lmTestHmnyRthm.pCopyLine =
  34.                 m_pstzParent.pmlnGetPrevLine(2,C);
  35.             sec.g_lmTestHmnyRthm.iCLOffset = 0;
  36.             pnote = m_arrpMusicLines[C].
  37.                 pNoteToPlay(iCurrBeatNum, sec.g_lmTestHmnyRthm);
  38.         }
  39.         if (pnote!=null)
  40.             return pnote;
  41.     }
  42.     // None of my lines wants to play anything, so...
  43.     return null;
  44. }
  45.  
  46. //-------------------------------
  47. public CNote pNoteToEnd (int iCurrBeatNum)
  48. {
  49.     for (int C=0; C<m_iNumLines; C++) {
  50.         CNote pNote = m_arrpMusicLines[C].
  51.             pNoteToEnd(iCurrBeatNum);
  52.         if (pNote!=null)
  53.             return pNote;
  54.     }
  55.     // None of my lines want to end anything so...
  56.     return null;
  57. }
  58.  
  59. //-------------------------------
  60. public void AppendAdditionalLine (
  61.         int iCenterNote)
  62. {    if (m_iNumLines == m_iNumAllocatedLines)
  63.     {    m_iNumAllocatedLines += 4;
  64.         CMusicLine[] temp = m_arrpMusicLines;
  65.         m_arrpMusicLines = new CMusicLine[m_iNumAllocatedLines];
  66.         for (int C=0; C<m_iNumAllocatedLines-4; C++)
  67.             m_arrpMusicLines[C] = temp[C];
  68.     }
  69.     m_iNumLines++;
  70.     assert(m_pstzParent.iNumLayers() >= m_iNumLines);
  71.     m_arrpMusicLines[m_iNumLines-1] = new
  72.         CMusicLine(this, m_pstzParent.plyrGetLayer(m_iNumLines-1));
  73.     assert(m_arrpMusicLines[m_iNumLines-1]!=null);
  74.     m_arrpMusicLines[m_iNumLines-1].SetCenterNote((char)iCenterNote);
  75. }
  76. //-------------------------------
  77. public CMusicLine pmlnGetLine (int index)
  78. {    assert(0<=index);
  79.     if (m_iNumLines <= index) return null;
  80.     return m_arrpMusicLines[index];
  81. }
  82. //-------------------------------
  83. public boolean bDonePlaying (int iCurrBeatNum)
  84. {    // This line's not over until the beat number is outside
  85.     // all my sub-lines.
  86.         
  87.     if (iMemoizedNumLines == m_iNumLines &&
  88.         iMemoizedBeatNum == iCurrBeatNum &&
  89.         pcmlMemoized == this)
  90.     {    //tw.spr("Memo'ed %d from %d %d %x\r", bMemoizedAnswer, m_iNumLines, iCurrBeatNum, this);
  91.         return (bMemoizedAnswer != 0);
  92.     }
  93.     iMemoizedBeatNum = iCurrBeatNum;
  94.     iMemoizedNumLines = m_iNumLines;
  95.     pcmlMemoized = this;
  96.     // Ignore subsequent changes in line rhythms.
  97.  
  98.     for (int C=0; C<m_iNumLines; C++)
  99.         if (!m_arrpMusicLines[C].bDonePlaying(iCurrBeatNum))
  100.         {    bMemoizedAnswer = 0;
  101.             //tw.spr("Memo'ing %d to %d %d %x\r", bMemoizedAnswer, m_iNumLines, iCurrBeatNum, this);
  102.             return (bMemoizedAnswer != 0);
  103.         }
  104.     bMemoizedAnswer = 1;
  105.     //tw.spr("Memo'ing %d to %d %d %x\r", bMemoizedAnswer, m_iNumLines, iCurrBeatNum, this);
  106.     return (bMemoizedAnswer != 0);
  107. }
  108. //-------------------------------
  109.     
  110. public void SetCadence (boolean bCad)    { m_bCadence = bCad; }
  111. public boolean bDoCadence () { return m_bCadence; }
  112.     public CScale sclGetScale ()
  113.     { return m_pstzParent.sclGetScale(); }
  114.     public char pvGetCohesion ()
  115.     { return m_pstzParent.pvGetCohesion(); }
  116.     public char pvGetKey ()
  117.     { return m_pstzParent.pvGetKey(); }
  118.  
  119. // Construction/Destruction
  120. CCompoundMusicLine (CStanza pstzParent)
  121. {
  122.     m_pstzParent=pstzParent; m_bCadence=false;
  123.     m_arrpMusicLines=null;
  124.     m_iNumLines=0; m_iNumAllocatedLines=0;
  125. }
  126. //-------------------------------
  127.     // Call this after creation and before you do anything
  128.     // else with this object:
  129. public void Initialize (int iNumSubLines/*=1*/)
  130. {    assert(iNumSubLines > 0);
  131.     for (int C=0; C<iNumSubLines; C++)
  132.     {    CLayer plyr = m_pstzParent.plyrGetLayer(C);
  133.         assert(plyr!=null);
  134.         AppendAdditionalLine((int)plyr.pvPitchMagnet());
  135.     }
  136. }
  137. //-------------------------------
  138. public void Initialize (CCompoundMusicLine pcmlReference)
  139. {    for (int C=0; C<pcmlReference.m_iNumLines; C++)
  140.     {    CLayer plyr = m_pstzParent.plyrGetLayer(C);
  141.         assert(plyr!=null);
  142.         CMusicLine pmlnRef = pcmlReference.pmlnGetLine(C);
  143.         char pvNewCenter = (char)
  144.             ((pmlnRef.pvGetCenterNote() + plyr.pvPitchMagnet()) / 2);
  145.         // Copying the reference line's rhythm, scale, and
  146.         // a related average pitch.
  147.         AppendAdditionalLine(pvNewCenter);
  148.     }
  149. }
  150.  
  151. }    // end class
  152. //========================================================
  153.