Debian Codegraph @

src/Examples/CsoundQt/Useful/MIDI_Recorder.csd

raw
1 <CsoundSynthesizer>
2 <CsOptions>
3 --midi-key=4 --midi-velocity=5 -+max_str_len=8192
4 </CsOptions>
5 <CsInstruments>
6 sr = 44100
7 ksmps = 64
8 nchnls = 2
9 0dbfs = 1.0
10
11 ;Jacob Joaquin
12 ;March 1, 2010
13 ;[email protected]
14 ;csound.noisepages.com
15
16 ; Tune Instruments
17 # define TUNE # 264.99816498389697 # ; 440 * 3 ^ (-6 / 13)
18
19 ; Instruments
20 # define MIDI_Synth # 1 #
21 # define Echo # 2 #
22 # define Capture_Events # 100 #
23 # define Playback # 200 #
24 # define Record # 201 #
25 # define Clear # 202 #
26 # define Init # 500 #
27
28 ; F-Tables
29 # define T_Sine # 1 # ; Sine wave
30
31 ; Sine wave
32 gitemp ftgen $T_Sine, 0, 2 ^ 16, 10, 1
33
34 gScore = ""
35 gkrecord init 0
36 gkoffset init 0
37 gkcounter init 0
38
39 instr $MIDI_Synth
40 ; MIDI input
41 inote_play = p4 ; Note number
42 ivelocity = p5 ; Velocity
43 ivelocity = ivelocity / 127 ; Normalize velocity
44 kgate madsr 0.005, 1, 1, 0.25 ; MIDI envelope
45
46 ; Frequency
47 ifreq = $TUNE * 3 ^ ((inote_play - 60) / 13)
48
49 ; FM Synth
50 a1 foscil kgate * ivelocity, ifreq, 1, 2, 6 * ivelocity, $T_Sine
51
52 ; Outputs
53 a1 = a1 * 0.5
54 outs a1, a1
55 chnmix a1, "Send"
56
57
58 ; Send event info to $Capture_Event
59 klisten init 1 ; Listen for release time
60 kflag release ; Track release
61 kdur timeinsts
62 ktime times
63
64 if (klisten == 1 && kflag == 1 && gkrecord == 1) then
65 event "i", $Capture_Events, 0, 1, ktime - gkoffset, kdur, inote_play, ivelocity * 127
66
67 ; Stop listening
68 klisten = 0
69 endif
70
71 endin
72
73 instr $Echo
74 iamp = p4 ; Amplitude
75 itime = p5 ; Time of delay
76 ifb = p6 ; Delay feedback amount
77 iroom_size = p7 ; Size of room, for reverb
78 idamp = p8 ; High frequency dampening of reverb
79
80 ; Receive send signal from MIDI_Synth
81 a0 chnget "Send"
82
83 ; Effects
84 adelay delayr itime
85 delayw a0 + adelay * ifb
86 al, ar freeverb adelay, adelay, 0.1, 0.2
87
88 ; Outputs
89 outs al * iamp, ar * iamp
90 chnclear "Send"
91
92 endin
93
94 instr $Capture_Events
95 istart = p4
96 idur = p5
97 inote = p6
98 ivelocity = p7
99
100 prints "captured -- i 1 %f %f %d %f\n", istart, idur, inote, \
101 ivelocity
102 Sline sprintf "i 1 %f %f %d %f\n", istart, \
103 idur, inote, ivelocity
104 fprints "captured.sco", Sline
105 gScore strcat gScore, Sline
106 gkcounter = gkcounter + 1
107 Snum sprintfk "%i", gkcounter
108 outvalue "numnotes", Snum
109 turnoff
110 endin
111
112 instr $Playback
113 S1 = gScore
114 linesleft:
115 ipos strindex S1, "\n"
116 if ipos == -1 igoto nomorelines
117 Sline strsub S1, 0, ipos
118 Stext strcat "playing -- ", Sline
119 prints Stext
120 prints "\n"
121 scoreline_i Sline
122 S1 strsub S1, ipos + 1
123 igoto linesleft
124 nomorelines:
125 endin
126
127 instr $Record
128 gkrecord = p4
129 gkoffset times
130 Srecording = ""
131 if p4 == 1 then
132 Srecording = "Recording"
133 endif
134 outvalue "recording", Srecording
135 endin
136
137 instr $Clear
138 gkrecord init 0
139 gkcounter init 0
140 gScore = ""
141 prints "Memory cleared."
142 turnoff
143 endin
144
145 instr $Init
146 outvalue "numnotes", "0"
147 outvalue "recording", ""
148 endin
149
150 </CsInstruments>
151 <CsScore>
152 ; Instruments
153 # define MIDI_Synth # 1 #
154 # define Echo # 2 #
155 # define Capture_Events # 100 #
156 # define Init # 500 #
157
158 ; Turn on echo effects
159 i $Echo 0 10000 1 0.333 0.333 0.2 0.2
160 i $Init 0 0.1
161
162 </CsScore>
163 </CsoundSynthesizer>
164
165
166 <MacOptions>
167 Version: 3
168 Render: Real
169 Ask: Yes
170 Functions: ioObject
171 Listing: Window
172 WindowBounds: 767 231 353 393
173 CurrentView: io
174 IOViewEdit: On
175 Options:
176 </MacOptions>
177 <MacGUI>
178 ioView background {57054, 58596, 65535}
179 ioButton {219, 247} {100, 30} event 1.000000 "button1" "Clear memory" "/" i202 0 1
180 ioButton {78, 300} {170, 30} event 1.000000 "button1" "Play events" "/" i200 0 -3600
181 ioButton {15, 247} {100, 30} event 1.000000 "button1" "Start Record" "/" i201 0 0.1 1
182 ioButton {117, 247} {100, 30} event 1.000000 "button1" "Pause Record" "/" i201 0 0.1 0
183 ioText {232, 221} {80, 25} display 0.000000 0.00100 "numnotes" center "Lucida Grande" 8 {0, 0, 0} {65280, 65280, 65280} nobackground border 5
184 ioText {15, 198} {203, 45} display 0.000000 0.00100 "recording" left "Lucida Grande" 20 {49920, 8192, 0} {65280, 65280, 65280} background border
185 ioText {221, 198} {102, 24} label 0.000000 0.00100 "" center "Lucida Grande" 8 {0, 0, 0} {65280, 65280, 65280} nobackground noborder Number recorded
186 ioText {24, 6} {292, 35} label 0.000000 0.00100 "" center "Lucida Grande" 20 {0, 0, 0} {65280, 65280, 65280} nobackground border MIDI Recorder
187 ioText {15, 47} {308, 134} label 0.000000 0.00100 "" left "Lucida Grande" 12 {0, 0, 0} {59648, 58368, 65280} background border This file can record incoming MIDI notes or realtime score events from the Live Event Panels, both to memory and to a csd file called "captured.sco". The file will be created in the current directory, so if you haven't saved this example, it can be created anywhere...
188 </MacGUI>
189
190 <EventPanel name="" tempo="60.00000000" loop="8.00000000" x="1200" y="349" width="445" height="314">i 1 0 0.255419 61 64.000000
191 </EventPanel>