forked from csound/csoundAPI_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerativeScoreExample.cpp
More file actions
130 lines (107 loc) · 3.47 KB
/
GenerativeScoreExample.cpp
File metadata and controls
130 lines (107 loc) · 3.47 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* Example 1 - Simple Compilation with Csound
Author: Steven Yi <stevenyi@gmail.com> 2013.10.28
Adapted for C++ by Rory Walsh
In this example, we will look at three techniques for generating our Score.
The first is one(SCO EXAMPLE1 ) we have already seen, which is to just write out the score
by hand as a String.
Knowing that we pass strings into Csound to pass note events, we can also
generate the string. In the second example(SCO EXAMPLE 2), sco2 starts as an empty string.
Using a for-loop, we append to sco2 note strings using the << operators.
The values are calculated using i which is the index of our for loop. The reslt of all this
is a score that represents an ascending note line.
In the final example(SCO EXAMPLE 3), we are going to generate a vector of 13 note values.
We then randomly choose what order to have the notes played back by randomly choosing
elements from the vector and adding it to our sco3 string.
The end result is a sequence of randomly selected notes from our note vector.
The final example represents a common pattern of development. For systems that
employ some event-based model of music, it is common to use some kind of data
structure to represent events. This may use some kind of common data structure
like a list, or it may be represented by using a class and instances of that
class.
Note, the three examples here are indicated with comments. To listen to the examples,
look for the lines that have csound->ReadScore(sco..) (lines 98-101), uncomment the one
you want to hear, and comment out the others.
*/
#include <iostream>
#include <iomanip>
#include <sstream>
#include "csound.hpp"
#include <string>
#include <vector>
#include "csPerfThread.hpp"
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
//ostringstream allows easy formatting of strings
std::ostringstream tmp;
std::string orc = "sr=44100\n\
ksmps=32\n\
nchnls=2\n\
0dbfs=1\n\
\n\
instr 1 \n\
ipch = cps2pch(p5, 12)\n\
kenv linsegr 0, .05, 1, .05, .7, .4, 0\n\
aout vco2 p4 * kenv, ipch \n\
aout moogladder aout, 2000, 0.25\n\
outs aout, aout\n\
endin";
/*
* SCORE EXAMPLE 1
* Creates a score using a single string
*/
string sco1 = "i1 0 1 .25 8.00";
/*
* SCORE EXAMPLE 2
* Create a score string using a for loop
*/
string sco2="";
for(int i=0;i<13;i++)
tmp << "i1 " << i*.25 << " .25 0.5 8." << setfill('0') << setw(2) << i << endl;
sco2 = tmp.str();
/*
* SCORE EXAMPLE 3
* Create a vector of note which then get played
* randomly by Csound
*/
vector<string> notes;
string sco3="";
for(int i=0;i<13;i++){
tmp.str("");
tmp.clear();
tmp << " .25 0.5 8." << setfill('0') << setw(2) << i << endl;
notes.push_back(tmp.str());
}
srand (time(NULL));
for(int i=0;i<20;i++){
tmp.str("");
tmp.clear();
tmp << "i1 " << i*.25 << notes[rand() % 13];
sco3+=tmp.str();
}
cout << sco3;
//create an instance of Csound
Csound* csound = new Csound();
//set CsOptions
csound->SetOption("-odac");
//compile orc
csound->CompileOrc(orc.c_str());
//compile score section
csound->ReadScore(sco1.c_str());
//csound->ReadScore(sco2.c_str());
//csound->ReadScore(sco3.c_str());
//prepare Csound for performance
csound->Start();
//set up CsoundPerfThread object
CsoundPerformanceThread* perfThread = new CsoundPerformanceThread(csound);
//start Csound performance
perfThread->Play();
//keep the application running while performance is ongoing
while(perfThread->GetStatus() == 0);
//free Csound and thread objects
delete csound;
delete perfThread;
return 0;
}