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 / CStanza.java < prev    next >
Encoding:
Java Source  |  2017-09-21  |  8.0 KB  |  245 lines

  1. public class CStanza
  2. {// Members
  3.     CSection m_psecParent;
  4.  
  5.     CLayer m_arrpLayers[];
  6.     int m_iNumLayers;
  7.     int m_iNumLayersAllocated;
  8.  
  9.     CCompoundMusicLine m_arrpCMLines[];
  10.     int m_iNumLines;
  11.     int m_iNumLinesAllocated;
  12.     int m_iMaxNumLines;
  13.  
  14. //======bogus global functions=====
  15. public int mod (int x, int y)
  16. {    if (x>=0) return x%y;
  17.     int iNegMod = (-x) % y;
  18.     return (0==iNegMod) ? y-iNegMod : 0;
  19. }
  20. public void assert(boolean b) { if (!b) m_arrpCMLines[-1] = null; }
  21. public int rand() { return (int)(Math.random() * 9747); }
  22. //================================
  23.  
  24. CStanza (CSection psecParent)
  25. {
  26.     m_arrpLayers=null; m_iNumLayers=0; m_iNumLayersAllocated=0;
  27.     m_arrpCMLines=null;
  28.     m_iNumLines=0; m_iNumLinesAllocated=0; m_iMaxNumLines=0;
  29.     m_psecParent=psecParent;
  30. }
  31.     
  32. //----------------------------
  33. // Call one of these after creation and before use:
  34. public void Initialize (CRhythm pRthm, int iMaxLines/*=4*/,
  35.         int iTempo/*=3*/)
  36. {    m_iMaxNumLines = iMaxLines;
  37.     assert(0==m_iNumLayers);
  38.     AppendNewLayer(pRthm,true);
  39.     bSetCurrTempo(iTempo,-1);
  40. }
  41. //----------------------------
  42. public void Initialize2 (CStanza pstz, int iMaxLines/*=4*/)
  43. {    m_iMaxNumLines = iMaxLines;
  44.     assert(0==m_iNumLayers);
  45.     for (int C=0; C<pstz.m_iNumLayers; C++)
  46.     {    AppendNewLayer(null,true);
  47.         m_arrpLayers[C].Set(pstz.m_arrpLayers[C]);
  48.     }
  49. }
  50.     
  51. // Operations
  52. //----------------------------
  53. public CNote pNoteToPlay (int iCurrBeatNum)
  54. {    if (0 == m_iNumLines ||
  55.         m_arrpCMLines[m_iNumLines-1].bDonePlaying(iCurrBeatNum))
  56.     {
  57.         AppendNewLine();    // Adding a line to the stanza.
  58.         if (m_iNumLines == 1)    // If first line of stanza...
  59.             m_arrpCMLines[m_iNumLines-1].Initialize(m_iNumLayers);
  60.         // Otherwise, use previous line as reference:
  61.         else m_arrpCMLines[m_iNumLines-1].
  62.             Initialize(m_arrpCMLines[m_iNumLines-2]);
  63.         // Update rhythm start times:
  64.         for (int C=0; C < m_iNumLayers; C++)
  65.             m_arrpLayers[C].pRhythm().SetFirstBeat(iCurrBeatNum);
  66.  
  67.         // Maybe a cadence at end of stanza:
  68.         boolean bCad = m_iNumLines == m_iMaxNumLines &&
  69.             mod(rand(),128) < m_psecParent.pvChangeRate();
  70.         m_arrpCMLines[m_iNumLines-1].SetCadence(bCad);
  71.         DetermineLineMods();    // Other calculations (Cf.)
  72.     }
  73.     return m_arrpCMLines[m_iNumLines-1].
  74.         pNoteToPlay(iCurrBeatNum);
  75. }
  76. //----------------------------
  77. public CNote pNoteToEnd (int iCurrBeatNum)
  78. {
  79.     if (0==m_iNumLines)
  80.         return null;
  81.     return m_arrpCMLines[m_iNumLines-1].
  82.         pNoteToEnd(iCurrBeatNum);
  83. }
  84. //----------------------------
  85. public boolean bDonePlaying (int iCurrBeatNum)
  86. {    if (m_iNumLines < m_iMaxNumLines)
  87.         return false;
  88.     return m_arrpCMLines[m_iNumLines-1].
  89.         bDonePlaying(iCurrBeatNum);
  90. }
  91.     
  92. //----------------------------
  93. public boolean bSetCurrTempo (int iNewTempo, int iLayerNum /*= -1*/)
  94. {
  95.     if (iLayerNum >= 0)
  96.     {    assert(iLayerNum < m_iNumLayers);
  97.         m_arrpLayers[iLayerNum].pRhythm().SetTempo((char)iNewTempo);
  98.         return true;
  99.     }
  100.     for (int C=0; C<m_iNumLayers; C++)
  101.         m_arrpLayers[C].pRhythm().SetTempo((char)iNewTempo);
  102.     return true;
  103. }
  104.  
  105. //----------------------------
  106. public boolean bSetCurrCenterNote (int iNewCenter)
  107. {    CMusicLine pmlnCurr = pmlnGetLine(m_iNumLines-1, 0);
  108.     if (null==pmlnCurr) return false;
  109.     pmlnCurr.SetCenterNote((char)iNewCenter);
  110.     return true;
  111. }
  112.  
  113. //----------------------------
  114. /*public int bSetRhythm (String szRthm, int iLayerIndex=0)
  115. {    if (iLayerIndex >= 0)
  116.     {    assert(iLayerIndex < m_iNumLayers);
  117.         return m_arrpLayers[iLayerIndex].pRhythm().bSet(szRthm);
  118.     }
  119.     for (int C=0; C<m_iNumLayers; C++)
  120.         if (!m_arrpLayers[C].pRhythm().bSet(szRthm))
  121.             return 0;
  122.     return 1;
  123. }
  124. */
  125. //----------------------------
  126. public boolean bSetRhythm (CRhythm pRthm, int iLayerIndex/* = 0*/)
  127. {    if (iLayerIndex >= 0)
  128.     {    assert(iLayerIndex < m_iNumLayers);
  129.         return m_arrpLayers[iLayerIndex].bSetRhythm(pRthm);
  130.     }
  131.     for (int C=0; C<m_iNumLayers; C++)
  132.         if (!m_arrpLayers[C].pRhythm().bSet(pRthm))
  133.             return false;
  134.     return true;
  135. }
  136. //----------------------------
  137.     public CRhythm rthmGetRhythm (int iLayerIndex/* = 0*/)
  138.     { return m_arrpLayers[iLayerIndex].pRhythm(); }
  139.     
  140.     public char pvGetKey () { return m_psecParent.pvGetKey(); }
  141.     public CScale sclGetScale ()
  142.     { return m_psecParent.sclGetScale(); }
  143.     public char pvGetCohesion ()
  144.     { return m_psecParent.pvGetCohesion(); }
  145. //----------------------------
  146.     // Return a given sub-line in the given compound line:
  147.     public CMusicLine pmlnGetLine (int iCompoundLineNum,
  148.         int iSubLineNum)
  149. {    assert(0 <= iCompoundLineNum && iCompoundLineNum < m_iNumLines);
  150.     if (m_iNumLines <= iCompoundLineNum) return null;
  151.     return m_arrpCMLines[iCompoundLineNum].
  152.         pmlnGetLine(iSubLineNum);
  153. }
  154. //----------------------------
  155.     // Same as pmlnGetLine, except counts backwards.
  156.     public CMusicLine pmlnGetPrevLine (int iBackNumLines,
  157.         int iSubLineNum)
  158. {    if (iBackNumLines <= 0 || m_iNumLines < iBackNumLines) return null;
  159.     return pmlnGetLine(m_iNumLines - iBackNumLines,
  160.         iSubLineNum);
  161. }
  162. //----------------------------
  163.     public CCompoundMusicLine pcmlGetCMLine (int iCompoundLineNum)
  164.     {    assert(0 <= iCompoundLineNum &&
  165.             iCompoundLineNum < m_iNumLines);
  166.         return m_arrpCMLines[iCompoundLineNum];    }
  167.     public CCompoundMusicLine pcmlGetPrevCMLine (int iBackNumLines/*=1*/)
  168.     {    return pcmlGetCMLine(m_iNumLines - iBackNumLines);    }
  169.     
  170.     public CLayer plyrGetLayer (int iLayerIndex)
  171.     {    assert(0 <= iLayerIndex && iLayerIndex < m_iNumLayers);
  172.         return m_arrpLayers[iLayerIndex];    }
  173.     public int iNumLayers () { return m_iNumLayers; }
  174. //----------------------------
  175. public void AppendNewLayer (CRhythm pRthm/*=0*/, boolean bActive/*=1*/)
  176. {    if (m_iNumLayers == m_iNumLayersAllocated)
  177.     {    m_iNumLayersAllocated += 3;
  178.         CLayer[] temp = m_arrpLayers;
  179.         m_arrpLayers = new CLayer[m_iNumLayersAllocated];
  180.         for (int C=0; C<m_iNumLayersAllocated-3; C++)
  181.             m_arrpLayers[C] = temp[C];
  182.     }
  183.     m_iNumLayers++;
  184.     if (null==pRthm)
  185.     {    if (m_iNumLayers > 1)
  186.             pRthm = m_arrpLayers[0].pRhythm();
  187.         else pRthm = m_psecParent.pGetRhythm(0);
  188.     }
  189.     assert(null!=pRthm);
  190.     m_arrpLayers[m_iNumLayers-1] = new CLayer(this, pRthm);
  191.     m_arrpLayers[m_iNumLayers-1].bSetChordMatch(m_iNumLayers!=1);
  192.     m_arrpLayers[m_iNumLayers-1].bSetActive(bActive);
  193. }
  194.     
  195. // Implementation
  196. //----------------------------
  197. void AppendNewLine ()
  198. {    if (m_iNumLines == m_iNumLinesAllocated)
  199.     {    m_iNumLinesAllocated += 6;
  200.         CCompoundMusicLine[] temp = m_arrpCMLines;
  201.         m_arrpCMLines = new CCompoundMusicLine[m_iNumLinesAllocated];
  202.         for (int C=0; C<m_iNumLinesAllocated-6; C++)
  203.             m_arrpCMLines[C] = temp[C];
  204.     }
  205.     m_iNumLines++;
  206.     m_arrpCMLines[m_iNumLines-1] = new CCompoundMusicLine(this);
  207. }
  208. //----------------------------
  209. void DetermineLineMods ()
  210. {    double fCoh = (double)(pvGetCohesion()) / 127.0;
  211.  
  212.     CSection sec=m_psecParent;
  213.     // Contrast some of the time:    
  214.     sec.g_lmTestMel.bContrast = false;
  215.     sec.g_lmTestHmnyRthm.bContrast = sec.g_lmTestMel.bContrast;
  216.  
  217.     // Should we copy a previous line?
  218.     if (!sec.g_lmTestMel.bContrast && 0!=mod(rand(), (int)(1.0+fCoh*16.0)))
  219.     {    // Copy some of the time (if not contrasting):
  220.         int iBackStz = (int)(1.5 + (double)(mod(rand(), 
  221.             m_psecParent.iNumStanzas())) * (1.0-fCoh));
  222.         if (iBackStz != 1 || m_iNumLines != 1) {
  223.             CStanza pStz = m_psecParent.pstzGetPrevStanza(iBackStz);
  224.             int iBack;
  225.             if (iBackStz == 1)
  226.                 iBack = (int)(2.5 + (double)(mod(rand(), m_iNumLines - 1)) *
  227.                     (1.0 - fCoh));
  228.             else iBack = (int)(1.5 + (double)(mod(rand(), pStz.m_iNumLines)) *
  229.                 (1.0 - fCoh));
  230.  
  231.             int iOff = (int)((double)(rand()%11 - 5) * (1.0-fCoh));
  232.             sec.g_lmTestMel.pCopyLine = 
  233.                 pStz.pmlnGetPrevLine(iBack, 0);    // Null if not found.
  234.             assert (null!=sec.g_lmTestMel.pCopyLine);
  235.             assert(sec.g_lmTestMel.pCopyLine.pvGetCenterNote() >= 0 &&
  236.                 sec.g_lmTestMel.pCopyLine.pvGetCenterNote() < 128);
  237.             int iCopyLen = sec.g_lmTestMel.pCopyLine.iGetNumNotesPlayed();
  238.             if (iCopyLen == 0) iCopyLen = 1;
  239.             sec.g_lmTestMel.iCopyIndex = (int)(iCopyLen-1-(1-fCoh)*(rand() % iCopyLen));
  240.             sec.g_lmTestMel.iCLOffset = iOff;
  241.         }
  242.     } else sec.g_lmTestMel.pCopyLine = null;    // Reset from prev val.
  243. }
  244.  
  245. }    // end class