home *** CD-ROM | disk | FTP | other *** search
Java Source | 2017-09-21 | 8.0 KB | 245 lines |
- public class CStanza
- {// Members
- CSection m_psecParent;
-
- CLayer m_arrpLayers[];
- int m_iNumLayers;
- int m_iNumLayersAllocated;
-
- CCompoundMusicLine m_arrpCMLines[];
- int m_iNumLines;
- int m_iNumLinesAllocated;
- int m_iMaxNumLines;
-
- //======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_arrpCMLines[-1] = null; }
- public int rand() { return (int)(Math.random() * 9747); }
- //================================
-
- CStanza (CSection psecParent)
- {
- m_arrpLayers=null; m_iNumLayers=0; m_iNumLayersAllocated=0;
- m_arrpCMLines=null;
- m_iNumLines=0; m_iNumLinesAllocated=0; m_iMaxNumLines=0;
- m_psecParent=psecParent;
- }
-
- //----------------------------
- // Call one of these after creation and before use:
- public void Initialize (CRhythm pRthm, int iMaxLines/*=4*/,
- int iTempo/*=3*/)
- { m_iMaxNumLines = iMaxLines;
- assert(0==m_iNumLayers);
- AppendNewLayer(pRthm,true);
- bSetCurrTempo(iTempo,-1);
- }
- //----------------------------
- public void Initialize2 (CStanza pstz, int iMaxLines/*=4*/)
- { m_iMaxNumLines = iMaxLines;
- assert(0==m_iNumLayers);
- for (int C=0; C<pstz.m_iNumLayers; C++)
- { AppendNewLayer(null,true);
- m_arrpLayers[C].Set(pstz.m_arrpLayers[C]);
- }
- }
-
- // Operations
- //----------------------------
- public CNote pNoteToPlay (int iCurrBeatNum)
- { if (0 == m_iNumLines ||
- m_arrpCMLines[m_iNumLines-1].bDonePlaying(iCurrBeatNum))
- {
- AppendNewLine(); // Adding a line to the stanza.
- if (m_iNumLines == 1) // If first line of stanza...
- m_arrpCMLines[m_iNumLines-1].Initialize(m_iNumLayers);
- // Otherwise, use previous line as reference:
- else m_arrpCMLines[m_iNumLines-1].
- Initialize(m_arrpCMLines[m_iNumLines-2]);
- // Update rhythm start times:
- for (int C=0; C < m_iNumLayers; C++)
- m_arrpLayers[C].pRhythm().SetFirstBeat(iCurrBeatNum);
-
- // Maybe a cadence at end of stanza:
- boolean bCad = m_iNumLines == m_iMaxNumLines &&
- mod(rand(),128) < m_psecParent.pvChangeRate();
- m_arrpCMLines[m_iNumLines-1].SetCadence(bCad);
- DetermineLineMods(); // Other calculations (Cf.)
- }
- return m_arrpCMLines[m_iNumLines-1].
- pNoteToPlay(iCurrBeatNum);
- }
- //----------------------------
- public CNote pNoteToEnd (int iCurrBeatNum)
- {
- if (0==m_iNumLines)
- return null;
- return m_arrpCMLines[m_iNumLines-1].
- pNoteToEnd(iCurrBeatNum);
- }
- //----------------------------
- public boolean bDonePlaying (int iCurrBeatNum)
- { if (m_iNumLines < m_iMaxNumLines)
- return false;
- return m_arrpCMLines[m_iNumLines-1].
- bDonePlaying(iCurrBeatNum);
- }
-
- //----------------------------
- public boolean bSetCurrTempo (int iNewTempo, int iLayerNum /*= -1*/)
- {
- if (iLayerNum >= 0)
- { assert(iLayerNum < m_iNumLayers);
- m_arrpLayers[iLayerNum].pRhythm().SetTempo((char)iNewTempo);
- return true;
- }
- for (int C=0; C<m_iNumLayers; C++)
- m_arrpLayers[C].pRhythm().SetTempo((char)iNewTempo);
- return true;
- }
-
- //----------------------------
- public boolean bSetCurrCenterNote (int iNewCenter)
- { CMusicLine pmlnCurr = pmlnGetLine(m_iNumLines-1, 0);
- if (null==pmlnCurr) return false;
- pmlnCurr.SetCenterNote((char)iNewCenter);
- return true;
- }
-
- //----------------------------
- /*public int bSetRhythm (String szRthm, int iLayerIndex=0)
- { if (iLayerIndex >= 0)
- { assert(iLayerIndex < m_iNumLayers);
- return m_arrpLayers[iLayerIndex].pRhythm().bSet(szRthm);
- }
- for (int C=0; C<m_iNumLayers; C++)
- if (!m_arrpLayers[C].pRhythm().bSet(szRthm))
- return 0;
- return 1;
- }
- */
- //----------------------------
- public boolean bSetRhythm (CRhythm pRthm, int iLayerIndex/* = 0*/)
- { if (iLayerIndex >= 0)
- { assert(iLayerIndex < m_iNumLayers);
- return m_arrpLayers[iLayerIndex].bSetRhythm(pRthm);
- }
- for (int C=0; C<m_iNumLayers; C++)
- if (!m_arrpLayers[C].pRhythm().bSet(pRthm))
- return false;
- return true;
- }
- //----------------------------
- public CRhythm rthmGetRhythm (int iLayerIndex/* = 0*/)
- { return m_arrpLayers[iLayerIndex].pRhythm(); }
-
- public char pvGetKey () { return m_psecParent.pvGetKey(); }
- public CScale sclGetScale ()
- { return m_psecParent.sclGetScale(); }
- public char pvGetCohesion ()
- { return m_psecParent.pvGetCohesion(); }
- //----------------------------
- // Return a given sub-line in the given compound line:
- public CMusicLine pmlnGetLine (int iCompoundLineNum,
- int iSubLineNum)
- { assert(0 <= iCompoundLineNum && iCompoundLineNum < m_iNumLines);
- if (m_iNumLines <= iCompoundLineNum) return null;
- return m_arrpCMLines[iCompoundLineNum].
- pmlnGetLine(iSubLineNum);
- }
- //----------------------------
- // Same as pmlnGetLine, except counts backwards.
- public CMusicLine pmlnGetPrevLine (int iBackNumLines,
- int iSubLineNum)
- { if (iBackNumLines <= 0 || m_iNumLines < iBackNumLines) return null;
- return pmlnGetLine(m_iNumLines - iBackNumLines,
- iSubLineNum);
- }
- //----------------------------
- public CCompoundMusicLine pcmlGetCMLine (int iCompoundLineNum)
- { assert(0 <= iCompoundLineNum &&
- iCompoundLineNum < m_iNumLines);
- return m_arrpCMLines[iCompoundLineNum]; }
- public CCompoundMusicLine pcmlGetPrevCMLine (int iBackNumLines/*=1*/)
- { return pcmlGetCMLine(m_iNumLines - iBackNumLines); }
-
- public CLayer plyrGetLayer (int iLayerIndex)
- { assert(0 <= iLayerIndex && iLayerIndex < m_iNumLayers);
- return m_arrpLayers[iLayerIndex]; }
- public int iNumLayers () { return m_iNumLayers; }
- //----------------------------
- public void AppendNewLayer (CRhythm pRthm/*=0*/, boolean bActive/*=1*/)
- { if (m_iNumLayers == m_iNumLayersAllocated)
- { m_iNumLayersAllocated += 3;
- CLayer[] temp = m_arrpLayers;
- m_arrpLayers = new CLayer[m_iNumLayersAllocated];
- for (int C=0; C<m_iNumLayersAllocated-3; C++)
- m_arrpLayers[C] = temp[C];
- }
- m_iNumLayers++;
- if (null==pRthm)
- { if (m_iNumLayers > 1)
- pRthm = m_arrpLayers[0].pRhythm();
- else pRthm = m_psecParent.pGetRhythm(0);
- }
- assert(null!=pRthm);
- m_arrpLayers[m_iNumLayers-1] = new CLayer(this, pRthm);
- m_arrpLayers[m_iNumLayers-1].bSetChordMatch(m_iNumLayers!=1);
- m_arrpLayers[m_iNumLayers-1].bSetActive(bActive);
- }
-
- // Implementation
- //----------------------------
- void AppendNewLine ()
- { if (m_iNumLines == m_iNumLinesAllocated)
- { m_iNumLinesAllocated += 6;
- CCompoundMusicLine[] temp = m_arrpCMLines;
- m_arrpCMLines = new CCompoundMusicLine[m_iNumLinesAllocated];
- for (int C=0; C<m_iNumLinesAllocated-6; C++)
- m_arrpCMLines[C] = temp[C];
- }
- m_iNumLines++;
- m_arrpCMLines[m_iNumLines-1] = new CCompoundMusicLine(this);
- }
- //----------------------------
- void DetermineLineMods ()
- { double fCoh = (double)(pvGetCohesion()) / 127.0;
-
- CSection sec=m_psecParent;
- // Contrast some of the time:
- sec.g_lmTestMel.bContrast = false;
- sec.g_lmTestHmnyRthm.bContrast = sec.g_lmTestMel.bContrast;
-
- // Should we copy a previous line?
- if (!sec.g_lmTestMel.bContrast && 0!=mod(rand(), (int)(1.0+fCoh*16.0)))
- { // Copy some of the time (if not contrasting):
- int iBackStz = (int)(1.5 + (double)(mod(rand(),
- m_psecParent.iNumStanzas())) * (1.0-fCoh));
- if (iBackStz != 1 || m_iNumLines != 1) {
- CStanza pStz = m_psecParent.pstzGetPrevStanza(iBackStz);
- int iBack;
- if (iBackStz == 1)
- iBack = (int)(2.5 + (double)(mod(rand(), m_iNumLines - 1)) *
- (1.0 - fCoh));
- else iBack = (int)(1.5 + (double)(mod(rand(), pStz.m_iNumLines)) *
- (1.0 - fCoh));
-
- int iOff = (int)((double)(rand()%11 - 5) * (1.0-fCoh));
- sec.g_lmTestMel.pCopyLine =
- pStz.pmlnGetPrevLine(iBack, 0); // Null if not found.
- assert (null!=sec.g_lmTestMel.pCopyLine);
- assert(sec.g_lmTestMel.pCopyLine.pvGetCenterNote() >= 0 &&
- sec.g_lmTestMel.pCopyLine.pvGetCenterNote() < 128);
- int iCopyLen = sec.g_lmTestMel.pCopyLine.iGetNumNotesPlayed();
- if (iCopyLen == 0) iCopyLen = 1;
- sec.g_lmTestMel.iCopyIndex = (int)(iCopyLen-1-(1-fCoh)*(rand() % iCopyLen));
- sec.g_lmTestMel.iCLOffset = iOff;
- }
- } else sec.g_lmTestMel.pCopyLine = null; // Reset from prev val.
- }
-
- } // end class