Debian Codegraph @

src/Examples/CsoundQt/Getting Started/Language_Features/Using_Udos.csd

raw
1 /* Getting started.. Writing User Defined Opcodes (UDO)
2
3 As described before, opcodes can be seen as little programs. So their usefulness can be exploited in a different way.
4 If you've build a structure which you like to use often, it may be convenient to pack it into an "User Defined Opcode". This way you can use your own constructions later by just one keyword.
5
6 The main advantages are:
7 - the project has a better structure
8 - copy and paste mistakes can be avoided
9 - the final code is smaller and easier to read
10 - it can be re-used in other contextes
11 - a correction in one place fixes
12
13 The keyword "opcode" opens the part for an user defined opcode, "endop" closes the part. Then you give your opcode it's name and define outputs and inputs.
14
15 In the following example, the Feedback-Delay used in Chapter 2 (MIDI Synth), is wrapped into an UDO. When this Echo effect is used in an Instrument, it is necessary to extend the duration by the time of echos. This extension is put into the UDO as well.
16
17 See: (OrchUDO SHIFT+F1), (opcode SHIFT+F1)
18 */
19
20 <CsoundSynthesizer>
21 <CsOptions>
22 </CsOptions>
23 <CsInstruments>
24
25
26 sr = 44100
27 ksmps = 128
28 nchnls = 2
29 0dbfs = 1
30
31 ; Feedback Delay which forces the instrument stay active as long as the delay plays
32 opcode MyFeedbackDelay,a,akki ; a= one audio output / akki= one audioinput, two k-rate inputs, one i-rate
33 ain, kDryWet, kDelTime, iFeedback xin ; Inputs: Audio-In, DryWet-Mix(0-1), Delay-Time(sec), Feedback-Amount (0-0.95)
34 iFeedback = (iFeedback > 0.95 ? 0.95 : iFeedback) ; limiting Feedback maximum to 0.95
35 aDelay delayr 1 ; a delayline, with 1 second maximum delay-time is initialised
36 aWet deltapi kDelTime ; data at a flexible position is read from the delayline
37 delayw ain+(aWet*iFeedback)
38 xout (1-kDryWet) * ain + (kDryWet * aWet) ; the amount of pure-signal and delayed-signal
39 iNumberOfDelays = (log(0.0004) / log(iFeedback)) ; number of repeats louder than 0.0004
40 xtratim p7*iNumberOfDelays ; forces the instrument to wait for X Delays
41 endop
42
43
44
45 instr 1 ; Sine Instrument
46 icps = p4
47 iamp = p5
48 aSrc oscili iamp, icps, 1
49 aEnv madsr 0.01, 0.1, 0.0, 0.01 ; percussive envelope
50 aDelayed init 0.0
51 kDryWet=p6
52 kDelTime=p7
53 iFeedback=p8
54 aDelayed MyFeedbackDelay aSrc*aEnv, kDryWet, kDelTime, iFeedback
55 outs aDelayed, aDelayed
56 endin
57 </CsInstruments>
58
59 <CsScore>
60 f 1 0 1024 10 1
61 ; nr st dur freq amp d/w d-time Fdbk
62 i 1 0 1 400 0.5 0.5 0.2 0.5
63 i 1 2 1 600 0.6 0.8 0.1 0.8
64 i 1 3 1 1200 0.3 0.2 0.6 0.3
65 i 1 4 1 800 0.3 0.2 0.4 0.8
66 i 1 4 1 400 0.3 0.2 0.6 0.8
67 i 1 6 1 300 0.3 0.2 0.5 0.9
68 e
69 </CsScore>
70 </CsoundSynthesizer>
71 ; written by Alex Hofmann (Mar. 2010) - Incontri HMT-Hannover
72
73 <bsbPanel>
74 <label>Widgets</label>
75 <objectName/>
76 <x>1013</x>
77 <y>279</y>
78 <width>563</width>
79 <height>397</height>
80 <visible>true</visible>
81 <uuid/>
82 <bgcolor mode="background">
83 <r>7</r>
84 <g>95</g>
85 <b>162</b>
86 </bgcolor>
87 <bsbObject version="2" type="BSBScope">
88 <objectName/>
89 <x>56</x>
90 <y>51</y>
91 <width>266</width>
92 <height>147</height>
93 <uuid>{6886a8e1-368e-408c-b028-88d9716ca673}</uuid>
94 <visible>true</visible>
95 <midichan>0</midichan>
96 <midicc>-3</midicc>
97 <value>-255.00000000</value>
98 <type>scope</type>
99 <zoomx>2.00000000</zoomx>
100 <zoomy>1.00000000</zoomy>
101 <dispx>1.00000000</dispx>
102 <dispy>1.00000000</dispy>
103 <mode>0.00000000</mode>
104 </bsbObject>
105 </bsbPanel>
106 <bsbPresets>
107 </bsbPresets>
108 <EventPanel name="" tempo="60.00000000" loop="8.00000000" x="323" y="111" width="616" height="389" visible="true" loopStart="0" loopEnd="0">i 1 1 1 400
109 i 1 2 1
110 i 1 3 1
111 i 1 4 1
112 hi
113 hi
114 hi
115 hi
116 hi hi
117 hi hi </EventPanel>