Debian Codegraph @

src/Examples/FLOSS Manual Examples/07 MIDI/07C06_RecordingController.csd

raw
1 <CsoundSynthesizer>
2 <CsOptions>
3 -odac -dm0
4 </CsOptions>
5 <CsInstruments>
6 sr = 44100
7 ksmps = 8
8 nchnls = 1
9 0dbfs = 1
10
11 FLpanel "Record Gesture",500,90,0,0
12 gkRecord,gihRecord FLbutton "Rec/Stop",1,0,22,100,25, 5, 5,-1
13 gkPlay,gihPlay FLbutton "Play", 1,0,22,100,25,110, 5,-1
14 gksave,ihsave FLbutton "Save to HD", 1,0,21,100,25,290,5,0,4,0,0
15 gkload,ihload FLbutton "Load from HD", 1,0,21,100,25,395,5,0,5,0,0
16 gkval, gihval FLslider "Control", 0,1, 0,23, -1,490,25, 5,35
17 FLpanel_end
18 FLrun
19
20 gidata ftgen 1,0,1048576,-2,0 ; Table for controller data.
21
22 opcode RecordController,0,Ki
23 kval,ifn xin
24 i_ ftgen 1,0,ftlen(ifn),-2,0 ; erase table
25 tableiw i(kval),1,ifn ; write initial value at index 1.
26 ;(Index 0 will be used be storing the complete gesture duration.)
27 kndx init 2 ; Initialise index
28 kTime timeinsts ; time since this instrument started in seconds
29 ; Write a data event only when the input value changes
30 if changed(kval)==1 && kndx<=(ftlen(ifn)-2) && kTime>0 then
31 ; Write timestamp to table location defined by current index.
32 tablew kTime, kndx, ifn
33 ; Write slider value to table location defined by current index.
34 tablew kval, kndx + 1, ifn
35 ; Increment index 2 steps (one for time, one for value).
36 kndx = kndx + 2
37 endif
38 ; sense note release
39 krel release
40 ; if we are in the final k-cycle before the note ends
41 if(krel==1) then
42 ; write total gesture duration into the table at index 0
43 tablew kTime,0,ifn
44 endif
45 endop
46
47 opcode PlaybackController,k,i
48 ifn xin
49 ; read first value
50 ; initial controller value read from index 1
51 ival table 1,ifn
52 ; initial value for k-rate output
53 kval init ival
54 ; Initialise index to first non-zero timestamp
55 kndx init 2
56 ; time in seconds since this note started
57 kTime timeinsts
58 ; first non-zero timestamp
59 iTimeStamp tablei 2,ifn
60 ; initialise k-variable for first non-zero timestamp
61 kTimeStamp init iTimeStamp
62 ; if we have reached the timestamp value...
63 if kTime>=kTimeStamp && kTimeStamp>0 then
64 ; ...Read value from table defined by current index.
65 kval table kndx+1,ifn
66 kTimeStamp table kndx+2,ifn ; Read next timestamp
67 ; Increment index. (Always 2 steps: timestamp and value.)
68 kndx limit kndx+2, 0, ftlen(ifn)-2
69 endif
70 xout kval
71 endop
72
73 ; cleaner way to start instruments than using FLbutton built-in mechanism
74 instr 1
75 ; trigger when button value goes from off to on
76 kOnTrig trigger gkRecord,0.5,0
77 ; start instrument with a held note when trigger received
78 schedkwhen kOnTrig,0,0,2,0,-1
79 ; trigger when button value goes from off to on
80 kOnTrig trigger gkPlay,0.5,0
81 ; start instrument with a held note when trigger received
82 schedkwhen kOnTrig,0,0,3,0,-1
83 endin
84
85 instr 2 ; Record gesture
86 if gkRecord==0 then ; If record button is deactivated...
87 turnoff ; ...turn this instrument off.
88 endif
89 ; call UDO
90 RecordController gkval,gidata
91 ; Generate a sound.
92 kporttime linseg 0,0.001,0.02
93 kval portk gkval,kporttime
94 asig poscil 0.2,cpsoct((kval*2)+7)
95 out asig
96
97 endin
98
99 instr 3 ; Playback recorded gesture
100 if gkPlay==0 then ; if play button is deactivated...
101 turnoff ; ...turn this instrument off.
102 endif
103 kval PlaybackController gidata
104 ; send initial value to controller
105 FLsetVal_i i(kval),gihval
106 ; Send values to slider when needed.
107 FLsetVal changed(kval),kval,gihval
108 ; Generate a sound.
109 kporttime linseg 0,0.001,0.02
110 kval portk gkval,kporttime
111 asig poscil 0.2,cpsoct((kval*2)+7)
112 out asig
113 ; stop note when end of table reached
114 kTime timeinsts ; time in seconds since this note began
115 ; read complete gesture duration from index zero
116 iRecTime tablei 0,gidata
117 ; if we have reach complete duration of gesture...
118 if kTime>=iRecTime then
119 ; deactivate play button (which will in turn, turn off this note.)
120 FLsetVal 1,0,gihPlay
121 endif
122 endin
123
124 instr 4 ; save table
125 ftsave "ControllerData.txt", 0, gidata
126 endin
127
128 instr 5 ; load table
129 ftload "ControllerData.txt", 0, gidata
130 endin
131
132 </CsInstruments>
133 <CsScore>
134 i 1 0 3600
135 </CsScore>
136 </CsoundSynthesizer>
137 ;example by Iain McCurdy