forked from csound/csoundAPI_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample6.cs
More file actions
72 lines (64 loc) · 2.97 KB
/
Example6.cs
File metadata and controls
72 lines (64 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using csound6netlib; //Exposes DotNet to Csound 6 Bridge's classes from csound6netlib.dll
namespace csoundAPI_examples
{
public partial class CsoundAPI_Examples
{
/* Example 6 - Generating a Score
*
* This example continues on from Example 5, rewriting the example using
* a Class called Note (in another class file for better reuse).
* The note example has its ToString() method implemented
* to generate a well-formatted Csound SCO note.
*
* This example also shows how a list of notes could be used multiple times.
* The first loop through we use the notes as-is, and during the second time
* we generate the notes again with the same properties except we alter the
* fifth p-field up 4 semitones.
*
* Note: Altering a Notes values like this is alright for this example, but
* it is a destructive edit. Real world code might make copies of Notes or
* alter the score generation to maintain the original values.
*/
public void Example6()
{
var r = new Random();
//Create score from a generated list of Note objects using Midi pitches 60-75.
var notes = new List<Note>();
for (int i = 0; i < 13; i++) // P1 P2-start P3-Dur P4-amp P5-frq
notes.Add(new Note(new double[] { 1, i * .25, .25, .5, r.Next(60, 75) }));
//Capture the note values as Csound parameters in a .net System.Text.StringBuilder
var sco = new StringBuilder();
foreach (var n in notes)
sco.AppendLine(n.ToString()); //note.ToString() will look like a score statement
//Add a major third and time displacement to the Note objects and put them in the score as well.
foreach (var n in notes)
{
if (n.Pfields.Length > 4) n.Pfields[4] += 4; //pitch (p5)
if (n.Pfields.Length > 1) n.Pfields[1] += .125; //start time (p2)
sco.AppendLine(n.ToString()); //More score statements with new freq and start
}
using (var c = new Csound6Net())
{
c.SetOption("-odac"); // Set option for Csound
try
{
//Now that the score has been generated, apply it to Csound:
c.CompileOrc(orc2); //Compile the matching orchestra
c.ReadScore(sco.ToString());//and apply the generated score
c.Start();
while (!c.PerformKsmps()) ;
c.Stop();
}
catch (Csound6NetException e)
{
System.Console.WriteLine(string.Format("Csound failed to complete: {0}", e.Message));
}
}
}
}
}