<!-- Converted to DocBook/SGML by Kevin Conder -->
<section id="CscoreAdvancd">
  <title>More Advanced Examples</title>

  <para>
    The following program demonstrates reading from two different input files. The idea is to switch between two 2-section scores, and write out the interleaved sections to a single output file.
  </para>

  <para>
    <informalexample>
      <programlisting>
./.htmlinclude &quot;cscore.h&quot;              /*   CSCORE_SWITCH.C  */ 
cscore()                        /* callable from either CSound or standalone cscore */ 
{ 
 EVLIST *a, *b; 
 FILE   *fp1, *fp2;         /* declare two scorefile stream pointers */ 
 fp1 = getcurfp();          /*  this is the command-line score */ 
 fp2 = filopen(&quot;score2.srt&quot;); /*  this is an additional score file */ 
 a = lget();                /* read section from score 1 */ 
 lput(a);                   /* write it out as is */ 
 putstr(&quot;s&quot;); 
 setcurfp(fp2); 
 b = lget();                /* read section from score 2 */ 
 lput(b);                   /* write it out as is */ 
 putstr(&quot;s&quot;); 
 lrelev(a);                 /* optional to reclaim space */ 
 lrelev(b); 
 setcurfp(fp1); 
 a = lget();                /* read next section from score 1 */ 
 lput(a);                   /* write it out */ 
 putstr(&quot;s&quot;); 
 setcurfp(fp2); 
 b = lget();                /* read next sect from score 2 */ 
 lput(b);                   / * write it out */ 
 putstr(&quot;e&quot;);
}
      </programlisting>
    </informalexample>
  </para>

  <para>
    Finally, we show how to take a literal, uninterpreted score file and imbue it with some expressive timing changes. The theory of composer-related metric pulses has been investigated at length by Manfred Clynes, and the following is in the spirit of his work. The strategy here is to first create an <emphasis>array</emphasis> of new <emphasis>onset </emphasis>times for every possible sixteenth-note onset, then to index into it so as to adjust the start and duration of each note of the input score to the interpreted time-points. This also shows how a<emphasis> </emphasis>Csound orchestra can be invoked repeatedly from a run-time score generator.
  </para>

  <para>
    <informalexample>
      <programlisting>
./.htmlinclude &quot;cscore.h&quot;              /*   CSCORE_PULSE.C  */ 
  
  /* program to apply interpretive durational pulse to     */ 
  /* an existing score in 3/4 time, first beats on 0, 3, 6 ... */
  
  
  static float four[4] = { 1.05, 0.97, 1.03, 0.95 };     /* pulse width for 4's*/ 
  static float three[3] = { 1.03, 1.05, .92 };           /* pulse width for 3's*/ 
  
   
cscore()          /* callable from either CSound or standalone cscore  */ 
{ 
  EVLIST  *a, *b; 
  register EVENT  *e, **ep; 
  float pulse16[4*4*4*4*3*4];  /* 16th-note array, 3/4 time, 256 measures */ 
  float acc16, acc1,inc1, acc3,inc3, acc12,inc12, acc48,inc48, acc192,inc192; 
  register float *p = pulse16; 
  register int  n16, n1, n3, n12, n48, n192; 

  /* fill the array with interpreted ontimes  */ 
  for (acc192=0.,n192=0; n192&lt;4; acc192+=192.*inc192,n192++) 
    for (acc48=acc192,inc192=four[n192],n48=0; n48&lt;4; acc48+=48.*inc48,n48++) 
      for (acc12=acc48,inc48=inc192*four[n48],n12=0;n12&lt;4; 
                                            acc12+=12.*inc12,n12++) 
        for (acc3=acc12,inc12=inc48*four[n12],n3=0; n3&lt;4; acc3+=3.*inc3,n3++) 
          for (acc1=acc3,inc3=inc12*four[n3],n1=0; n1&lt;3; acc1+=inc1,n1++) 
            for (acc16=acc1,inc1=inc3*three[n1],n16=0; n16&lt;4; 
                                            acc16+=.25*inc1*four[n16],n16++) 
              *p++ = acc16; 
   
  
  /* for (p = pulse16, n1 = 48; n1--; p += 4)  /* show vals &amp; diffs */ 
  /*   printf(&quot;%g %g %g %g %g %g %g %g\n&quot;, *p, *(p+1), *(p+2), *(p+3), 
  /*  *(p+1)-*p, *(p+2)-*(p+1), *(p+3)-*(p+2), *(p+4)-*(p+3)); */ 
  
  a = lget();             /* read sect from tempo-warped score */ 
  b = lseptwf(a);         /* separate warp &amp; fn statements */ 
  lplay(b);               /*   and send these to performance */ 
  a = lappstrev(a, &quot;s&quot;);  /* append a sect statement to note list */ 
  lplay(a);               /* play the note-list without interpretation */ 
  for (ep = &amp;a-e[1], n1 = a-nevents; n1--; ) { /* now pulse-modifiy it */ 
    e = *ep++; 
    if (e-op == 'i') { 
      e-p[2] = pulse16[(int)(4. * e-p2orig)]; 
      e-p[3] = pulse16[(int)(4. * (e-p2orig + e-p3orig))] - e-p[2]; 
    } 
  } 
  
  lplay(a);   /* now play modified list */
}
      </programlisting>
    </informalexample>
  </para>

  <para>
    As stated above, the input files to <emphasis>Cscore</emphasis> may be in original or time-warped and pre-sorted form; this modality will be preserved (section by section) in reading, processing and writing scores. Standalone processing will most often use unwarped sources and create unwarped new files. When running from within Csound the input score will arrive already warped and sorted, and can thus be sent directly (normally section by section) to the orchestra.
  </para>

  <para>
    A list of events can be conveyed to a Csound<emphasis> </emphasis>orchestra using <emphasis>lplay</emphasis>. There may be any number of <emphasis>lplay </emphasis>calls in a <emphasis>Cscore</emphasis> program. Each list so conveyed can be either time-warped or not, but each list must be in strict <emphasis>p2</emphasis>-chronological order (either from presorting or using <emphasis>lsort</emphasis>). If there is no <emphasis>lplay</emphasis> in a <emphasis>Cscore</emphasis> module run from within Csound, all events written out (via <emphasis>putev, putstr or lput</emphasis>) constitute a new score, which will be sent initially to <emphasis>scsort</emphasis> then to the Csound<emphasis> </emphasis>orchestra for performance. These can be examined in the files <quote><emphasis>cscore.out</emphasis></quote> and <quote><emphasis>cscore.srt</emphasis></quote>.
  </para>

  <para>
    A standalone cscore program will normally use the put commands to write into its output file. If a standalone <emphasis>Cscore</emphasis> program contains <emphasis>lplay</emphasis>, the events thus intended for performance will instead be printed on the console.
  </para>

  <para>
    A note list sent by <emphasis>lplay</emphasis> for performance should be temporally distinct from subsequent note lists. No note-end should extend past the next list's start time, since <emphasis>lplay</emphasis> will complete each list before starting the next (i.e. like a Section marker that doesn't reset local time to zero). This is important when using <emphasis>lgetnext()</emphasis> or <emphasis>lgetuntil()</emphasis>to fetch and process score segments prior to performance.
  </para>
</section>
