forked from csound/csoundAPI_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample5.vb
More file actions
101 lines (80 loc) · 4.56 KB
/
Example5.vb
File metadata and controls
101 lines (80 loc) · 4.56 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
Imports System.Text
Imports System.Collections.Generic
Imports csound6netlib
Partial Public Class Examples
' Example 5 - Generating a Score, VB Try/Catch blocks
' In this example, we will look at three techniques for generating our Score.
' The first is one 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, the score starts as an empty string.
' Using a for-loop, we append to sco note strings using a string formatting
' string that has its replacement values replaced. The replace values are
' calculated using the i value, and the result is an ascending note line.
' In the final example, we are going to generate a list of lists. The top-level
' list represents our score as a whole, and each sub-list within it represents
' the data for a single note. The main list is then processed in two ways: first,
' it processes each sub-list and joins the values together into a single note string;
' second, it joins each individual note string into a single, large score string,
' separated by newlines. The end result is a sequence of 13 notes with random
' pitches.
' 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 within case statement. To listen to the examples,
' use a real number when selecting Example 5: 5.0 for the single note, 5.1 for the
' chromatic scale and 5.2 for random pitches.
Public Sub Example5(algorithm As Integer)
Dim sco As New StringBuilder
'Choose one of 3 ways to create a score: sequential pitches, random pitches, or hard-coded
Select Case algorithm 'integer between 0 and 2
Case 1
For i = 0 To 12
sco.AppendLine(String.Format("i1 {0} .25 0.5 8.{1:00}", (i * 0.25), i))
Next i
Case 2
Dim vals = New List(Of Double()) 'initialize a list to hold lists of doubles
Dim r = New Random()
For i = 0 To 12 'populate that list with note parameters (pitch is random number from 0 to 14
vals.Add(New Double() {i * 0.25, 0.25, 0.5, r.Next(15)})
Next i
' convert list of lists into a list of strings with random frequencys
For Each val As Double() In vals
sco.AppendLine(String.Format("i1 {0} {1} {2} 8.{3:00}", val(0), val(1), val(2), CInt(val(3))))
Next val
Case Else
sco.AppendLine("i1 0 1 0.5 8.00")
End Select
' Once a score string has been algorithmically assembled, do the usual csound loop.
' This time, we'll let C# Exceptions (try/catch) handle testing status for us.
' By catching the exception, we are assured exiting the "using" block
' and thereby disposing of csound and its memory correctly.
Using c As New Csound6Net
c.SetOutputDac(0) ' Set realtime output
Try
c.CompileOrc(orc2) ' Compile different Orchestra with moogladder
c.ReadScore(sco.ToString()) ' Read in score as built above
c.Start() ' When compiling from strings, this call needed before performing
While c.PerformKsmps() = False 'Play score until csound says it is over
End While
c.Stop()
Catch ex As Csound6NetException
Console.WriteLine(ex.Message)
End Try
End Using
End Sub
'Used in examples 5 and 6
Public Const orc2 = "sr=44100" & vbCrLf & _
"ksmps=32" & vbCrLf & _
"nchnls=2" & vbCrLf & _
"0dbfs=1" & vbCrLf & _
"instr 1 " & vbCrLf & _
"ipch = cps2pch(p5, 12)" & vbCrLf & _
"kenv linsegr 0, .05, 1, .05, .7, .4, 0" & vbCrLf & _
"aout vco2 p4 * kenv, ipch" & vbCrLf & _
"aout moogladder aout, 2000, 0.25" & vbCrLf & _
"outs aout, aout" & vbCrLf & _
"endin"
End Class