home *** CD-ROM | disk | FTP | other *** search
Java Source | 2017-09-21 | 4.7 KB | 153 lines |
-
- //========================================================
- // Multiple musical lines played at the same time:
- public class CCompoundMusicLine // Hungarian: cml
- {
- // Members
- CStanza m_pstzParent;
- boolean m_bCadence;
- CMusicLine m_arrpMusicLines[];
- int m_iNumLines;
- int m_iNumAllocatedLines;
-
- // Memoization:
- int iMemoizedBeatNum = -1;
- int iMemoizedNumLines = -1;
- int bMemoizedAnswer = -1;
- CCompoundMusicLine pcmlMemoized = null;
-
- public void assert(boolean b) { if (!b) m_arrpMusicLines[-1] = null; }
-
- // Operations
- //-------------------------------
- public CNote pNoteToPlay (int iCurrBeatNum)
- { assert(m_iNumLines > 0);
- CSection sec = m_pstzParent.m_psecParent;
- for (int C=0; C<m_iNumLines; C++)
- { CNote pnote;
- if (C==0)
- { // Melody:
- pnote = m_arrpMusicLines[C].
- pNoteToPlay(iCurrBeatNum, sec.g_lmTestMel);
- } else // Harmony:
- { sec.g_lmTestHmnyRthm.pCopyLine =
- m_pstzParent.pmlnGetPrevLine(2,C);
- sec.g_lmTestHmnyRthm.iCLOffset = 0;
- pnote = m_arrpMusicLines[C].
- pNoteToPlay(iCurrBeatNum, sec.g_lmTestHmnyRthm);
- }
- if (pnote!=null)
- return pnote;
- }
- // None of my lines wants to play anything, so...
- return null;
- }
-
- //-------------------------------
- public CNote pNoteToEnd (int iCurrBeatNum)
- {
- for (int C=0; C<m_iNumLines; C++) {
- CNote pNote = m_arrpMusicLines[C].
- pNoteToEnd(iCurrBeatNum);
- if (pNote!=null)
- return pNote;
- }
- // None of my lines want to end anything so...
- return null;
- }
-
- //-------------------------------
- public void AppendAdditionalLine (
- int iCenterNote)
- { if (m_iNumLines == m_iNumAllocatedLines)
- { m_iNumAllocatedLines += 4;
- CMusicLine[] temp = m_arrpMusicLines;
- m_arrpMusicLines = new CMusicLine[m_iNumAllocatedLines];
- for (int C=0; C<m_iNumAllocatedLines-4; C++)
- m_arrpMusicLines[C] = temp[C];
- }
- m_iNumLines++;
- assert(m_pstzParent.iNumLayers() >= m_iNumLines);
- m_arrpMusicLines[m_iNumLines-1] = new
- CMusicLine(this, m_pstzParent.plyrGetLayer(m_iNumLines-1));
- assert(m_arrpMusicLines[m_iNumLines-1]!=null);
- m_arrpMusicLines[m_iNumLines-1].SetCenterNote((char)iCenterNote);
- }
- //-------------------------------
- public CMusicLine pmlnGetLine (int index)
- { assert(0<=index);
- if (m_iNumLines <= index) return null;
- return m_arrpMusicLines[index];
- }
- //-------------------------------
- public boolean bDonePlaying (int iCurrBeatNum)
- { // This line's not over until the beat number is outside
- // all my sub-lines.
-
- if (iMemoizedNumLines == m_iNumLines &&
- iMemoizedBeatNum == iCurrBeatNum &&
- pcmlMemoized == this)
- { //tw.spr("Memo'ed %d from %d %d %x\r", bMemoizedAnswer, m_iNumLines, iCurrBeatNum, this);
- return (bMemoizedAnswer != 0);
- }
- iMemoizedBeatNum = iCurrBeatNum;
- iMemoizedNumLines = m_iNumLines;
- pcmlMemoized = this;
- // Ignore subsequent changes in line rhythms.
-
- for (int C=0; C<m_iNumLines; C++)
- if (!m_arrpMusicLines[C].bDonePlaying(iCurrBeatNum))
- { bMemoizedAnswer = 0;
- //tw.spr("Memo'ing %d to %d %d %x\r", bMemoizedAnswer, m_iNumLines, iCurrBeatNum, this);
- return (bMemoizedAnswer != 0);
- }
- bMemoizedAnswer = 1;
- //tw.spr("Memo'ing %d to %d %d %x\r", bMemoizedAnswer, m_iNumLines, iCurrBeatNum, this);
- return (bMemoizedAnswer != 0);
- }
- //-------------------------------
-
- public void SetCadence (boolean bCad) { m_bCadence = bCad; }
- public boolean bDoCadence () { return m_bCadence; }
- public CScale sclGetScale ()
- { return m_pstzParent.sclGetScale(); }
- public char pvGetCohesion ()
- { return m_pstzParent.pvGetCohesion(); }
- public char pvGetKey ()
- { return m_pstzParent.pvGetKey(); }
-
- // Construction/Destruction
- CCompoundMusicLine (CStanza pstzParent)
- {
- m_pstzParent=pstzParent; m_bCadence=false;
- m_arrpMusicLines=null;
- m_iNumLines=0; m_iNumAllocatedLines=0;
- }
- //-------------------------------
- // Call this after creation and before you do anything
- // else with this object:
- public void Initialize (int iNumSubLines/*=1*/)
- { assert(iNumSubLines > 0);
- for (int C=0; C<iNumSubLines; C++)
- { CLayer plyr = m_pstzParent.plyrGetLayer(C);
- assert(plyr!=null);
- AppendAdditionalLine((int)plyr.pvPitchMagnet());
- }
- }
- //-------------------------------
- public void Initialize (CCompoundMusicLine pcmlReference)
- { for (int C=0; C<pcmlReference.m_iNumLines; C++)
- { CLayer plyr = m_pstzParent.plyrGetLayer(C);
- assert(plyr!=null);
- CMusicLine pmlnRef = pcmlReference.pmlnGetLine(C);
- char pvNewCenter = (char)
- ((pmlnRef.pvGetCenterNote() + plyr.pvPitchMagnet()) / 2);
- // Copying the reference line's rhythm, scale, and
- // a related average pitch.
- AppendAdditionalLine(pvNewCenter);
- }
- }
-
- } // end class
- //========================================================
-