Debian Codegraph @

src/Examples/McCurdy Collection/LiveSampling/TableRecBasic.csd

raw
1 ;Written by Iain McCurdy, 2009
2
3 ; Modified for QuteCsound by René, January 2011
4 ; Tested on Ubuntu 10.04 with csound-float 5.13.0 and QuteCsound svn rev 817
5
6 ;Notes on modifications from original csd:
7 ; Macro SWITCH and modifications in instruments to get the same behaviour as FLTK button
8 ; Pause change to avoid high cpu load
9
10
11 ;my flags on Ubuntu: -dm0 -iadc -odac -+rtaudio=jack -b16 -B4096 --expression-opt -+rtmidi=null
12 <CsoundSynthesizer>
13 <CsOptions>
14
15 </CsOptions>
16 <CsInstruments>
17 sr = 44100 ;SAMPLE RATE
18 ksmps = 16 ;NUMBER OF AUDIO SAMPLES IN EACH CONTROL CYCLE
19 nchnls = 2 ;NUMBER OF CHANNELS (2=STEREO)
20
21
22 giaudio ftgen 0, 0, 1048576, -7, 0, 1048576, 0 ;AUDIO DATA STORAGE
23
24 gkrecdur init 0
25
26
27 #define SWITCH(Name'Value'Instr_Led'Instr_Event)
28 #
29 ;To have a latched+event+value button *****************************************
30 strset $Value, "$Name_L"
31 k$Name init 0
32
33 kButton invalue "$Name_B"
34 kB_ON trigger kButton, 0.5, 0
35
36 if (k$Name == 0) then
37 schedkwhen kB_ON, 0, 0, $Instr_Led, 0, 0, $Value, 1 ;Led ON
38 schedkwhen kB_ON, 0, 0, $Instr_Event, 0, -1 ;Start instr
39 else
40 schedkwhen kB_ON, 0, 0, $Instr_Led, 0, 0, $Value, 0 ;Led OFF
41 schedkwhen kB_ON, 0, 0, -$Instr_Event, 0, 1 ;Stop instr
42 endif
43 k$Name invalue "$Name_L"
44 ;******************************************************************************
45 #
46
47
48 instr 1 ;GUI
49 $SWITCH(Record'100'2'11)
50 $SWITCH(PlayStop'200'2'13)
51
52 gkPause invalue "Pause"
53 gkSpeed invalue "Speed"
54 gkLoopBeg invalue "LoopBeg"
55 gkLoopEnd invalue "LoopEnd"
56 endin
57
58 instr 2 ;LED ON/OFF
59 Sp4 strget p4
60 outvalue Sp4, p5
61 endin
62
63 instr 11 ;RECORD
64 if gkPause = 1 then ;IF PAUSE BUTTON IS ACTIVATED...
65 kgoto END ;GOTO 'END' LABEL
66 endif ;END OF CONDITIONAL BRANCHING
67
68 itablelen tableng giaudio
69
70 ain inch 1 ;READ AUDIO FROM LIVE INPUT CHANNEL 1
71 andx line 0, itablelen/sr, 1 ;CREATE A POINTER FOR WRITING TO TABLE - FREQUENCY OF POINTER IS DEPENDENT UPON TABLE LENGTH AND SAMPLE RATE
72 andx = andx * itablelen ;RESCALE POINTER ACCORDING TO LENGTH OF FUNCTION TABLE
73 gkrecdur downsamp andx ;CREATE A K-RATE GLOBAL VARIABLE THAT WILL BE USED BY THE 'PLAYBACK' INSTRUMENT TO DETERMINE THE LENGTH OF RECORDED DATA
74
75 ;OPCODE INPUT | INDEX | TABLE
76 tablew ain, andx, giaudio ;WRITE AUDIO TO AUDIO STORAGE TABLE
77
78 if gkrecdur >= itablelen then ;IF MAXIMUM RECORD TIME IS REACHED...
79 schedkwhen 1, 0, 0, 2, 0, 0, 100, 0 ;Record Led OFF
80 turnoff ;TURN OFF THIS INSTRUMENT IMMEDIATELY.
81 endif ;END OF CONDITIONAL BRANCHING
82 END:
83 endin
84
85 instr 12 ;PLAYBACK LOOPED INSTRUMENT
86 if gkPause = 1 then ;IF PAUSE BUTTON IS ACTIVATED...
87 kgoto END ;GOTO 'END' LABEL
88 endif ;END OF CONDITIONAL BRANCHING
89
90 iporttime = 0.02
91 kporttime linseg 0,0.001,iporttime,1,iporttime
92 kLoopBeg portk gkLoopBeg, kporttime
93 kLoopEnd portk gkLoopEnd, kporttime
94
95 kLoopBeg = kLoopBeg * gkrecdur ;RESCALE gkLoopBeg (RANGE 0-1) TO BE WITHIN THE RANGE 0-FILE_LENGTH. NEW OUTPUT VARIABLE kLoopBeg.
96 kLoopEnd = kLoopEnd * gkrecdur ;RESCALE gkLoopEnd (RANGE 0-1) TO BE WITHIN THE RANGE 0-FILE_LENGTH. NEW OUTPUT VARIABLE kLoopEnd.
97 kLoopLen = kLoopEnd - kLoopBeg ;DERIVE LOOP LENGTH FROM LOOP START AND END POINTS
98
99 ; OPCODE NUMERATOR | DENOMINATOR | SUBSTUTION_VALUE_(IF_DENOMINATOR=0)
100 kPhasFrq divz gkSpeed, (kLoopLen/sr), .00001 ;SAFELY DIVIDE, PROVIDING ALTERNATIVE VALUE INCASE DENOMINATOR IS ZERO
101 andx phasor kPhasFrq ;DEFINE PHASOR POINTER FOR TABLE INDEX
102 kLoopBeg = (kLoopBeg < kLoopEnd ? kLoopBeg : kLoopEnd) ;CHECK IF LOOP-BEGINNING AND LOOP-END SLIDERS HAVE BEEN REVERSED
103 andx = (andx*abs(kLoopLen)) + kLoopBeg ;RESCALE INDEX POINTER ACCORDING TO LOOP LENGTH AND LOOP BEGINING
104
105 ;OUT OPCODE INDEX | FUNCTION_TABLE
106 asig tablei andx, giaudio ;READ AUDIO FROM AUDIO STORAGE FUNCTION TABLE
107 outs asig, asig ;SEND AUDIO TO OUTPUTS
108 END:
109 endin
110
111 instr 13 ;PLAY THEN STOP
112 if gkPause = 1 then ;IF PAUSE BUTTON IS ACTIVATED...
113 kgoto END ;GOTO 'END' LABEL
114 endif ;END OF CONDITIONAL BRANCHING
115
116 kLoopBeg = gkLoopBeg * gkrecdur ;RESCALE gkLoopBeg (RANGE 0-1) TO BE WITHIN THE RANGE 0-FILE_LENGTH. NEW OUTPUT VARIABLE kLoopBeg.
117 kLoopEnd = gkLoopEnd * gkrecdur ;RESCALE gkLoopEnd (RANGE 0-1) TO BE WITHIN THE RANGE 0-FILE_LENGTH. NEW OUTPUT VARIABLE kLoopEnd.
118
119 if kLoopEnd > kLoopBeg then
120 andx line 0,1,1
121 andx = (andx*gkSpeed*sr)+kLoopBeg
122 kndx downsamp andx ;CREATE kndx, A K-RATE VERSION OF andx
123 if kndx > kLoopEnd then ;IF END OF RECORDING IS REACHED...
124 schedkwhen 1, 0, 0, 2, 0, 0, 200, 0 ;PlayStop Led OFF
125 turnoff ;TURN OFF THIS INSTRUMENT IMMEDIATELY.
126 endif
127 else
128 andx line 0,1,-1
129 andx = (andx*gkSpeed*sr)+kLoopBeg
130 kndx downsamp andx ;CREATE kndx, A K-RATE VERSION OF andx
131 if kndx < kLoopEnd then ;IF END OF RECORDING IS REACHED...
132 schedkwhen 1, 0, 0, 2, 0, 0, 200, 0 ;PlayStop Led OFF
133 turnoff ;TURN OFF THIS INSTRUMENT IMMEDIATELY.
134 endif
135 endif ;END OF CONDITIONAL BRANCHING
136
137 ;OUT OPCODE INDEX | FUNCTION_TABLE
138 asig tablei andx, giaudio ;READ AUDIO FROM AUDIO STORAGE FUNCTION TABLE
139 outs asig, asig ;SEND AUDIO TO OUTPUTS
140 END:
141 endin
142 </CsInstruments>
143 <CsScore>
144 i 1 0 3600 ;GUI
145 </CsScore>
146 </CsoundSynthesizer>
147 <bsbPanel>
148 <label>Widgets</label>
149 <objectName/>
150 <x>72</x>
151 <y>179</y>
152 <width>400</width>
153 <height>200</height>
154 <visible>true</visible>
155 <uuid/>
156 <bgcolor mode="nobackground">
157 <r>202</r>
158 <g>202</g>
159 <b>202</b>
160 </bgcolor>
161 <bsbObject version="2" type="BSBLabel">
162 <objectName/>
163 <x>2</x>
164 <y>2</y>
165 <width>512</width>
166 <height>260</height>
167 <uuid>{395a9956-93f5-441f-b0ef-3828f1b2da1c}</uuid>
168 <visible>true</visible>
169 <midichan>0</midichan>
170 <midicc>0</midicc>
171 <label>Record Audio to Function Table With Playback</label>
172 <alignment>center</alignment>
173 <font>Liberation Sans</font>
174 <fontsize>18</fontsize>
175 <precision>3</precision>
176 <color>
177 <r>0</r>
178 <g>0</g>
179 <b>0</b>
180 </color>
181 <bgcolor mode="background">
182 <r>181</r>
183 <g>199</g>
184 <b>255</b>
185 </bgcolor>
186 <bordermode>border</bordermode>
187 <borderradius>5</borderradius>
188 <borderwidth>2</borderwidth>
189 </bsbObject>
190 <bsbObject version="2" type="BSBButton">
191 <objectName>Record_B</objectName>
192 <x>8</x>
193 <y>42</y>
194 <width>80</width>
195 <height>30</height>
196 <uuid>{c54faf18-3b5b-4a78-b803-69b1c339890e}</uuid>
197 <visible>true</visible>
198 <midichan>0</midichan>
199 <midicc>0</midicc>
200 <type>value</type>
201 <pressedValue>1.00000000</pressedValue>
202 <stringvalue/>
203 <text> Rec</text>
204 <image>/</image>
205 <eventLine/>
206 <latch>false</latch>
207 <latched>false</latched>
208 </bsbObject>
209 <bsbObject version="2" type="BSBButton">
210 <objectName>Pause</objectName>
211 <x>98</x>
212 <y>42</y>
213 <width>80</width>
214 <height>30</height>
215 <uuid>{63b09b32-866d-4b31-ba3c-fb6f456359dc}</uuid>
216 <visible>true</visible>
217 <midichan>0</midichan>
218 <midicc>0</midicc>
219 <type>value</type>
220 <pressedValue>1.00000000</pressedValue>
221 <stringvalue/>
222 <text> Pause</text>
223 <image>/</image>
224 <eventLine/>
225 <latch>true</latch>
226 <latched>false</latched>
227 </bsbObject>
228 <bsbObject version="2" type="BSBLabel">
229 <objectName/>
230 <x>516</x>
231 <y>2</y>
232 <width>480</width>
233 <height>260</height>
234 <uuid>{10e7ae72-c151-4c9d-9f49-e6b1585d8386}</uuid>
235 <visible>true</visible>
236 <midichan>0</midichan>
237 <midicc>0</midicc>
238 <label>Record Audio to Function Table With Playback</label>
239 <alignment>center</alignment>
240 <font>Liberation Sans</font>
241 <fontsize>18</fontsize>
242 <precision>3</precision>
243 <color>
244 <r>0</r>
245 <g>0</g>
246 <b>0</b>
247 </color>
248 <bgcolor mode="background">
249 <r>181</r>
250 <g>199</g>
251 <b>255</b>
252 </bgcolor>
253 <bordermode>border</bordermode>
254 <borderradius>5</borderradius>
255 <borderwidth>2</borderwidth>
256 </bsbObject>
257 <bsbObject version="2" type="BSBLabel">
258 <objectName/>
259 <x>517</x>
260 <y>17</y>
261 <width>477</width>
262 <height>244</height>
263 <uuid>{97dfe67c-26c1-4af5-b858-e301d6886c2e}</uuid>
264 <visible>true</visible>
265 <midichan>0</midichan>
266 <midicc>0</midicc>
267 <label>---------------------------------------------------------------------------------------------------------------------
268 This example records live audio to a function table which the user can then play back. A pause button pauses both record and playback.
269 If record is pressed for a second time the original recording is overwritten. Maximum record time is determined by function table size and sampling rate. In this example table size is 1048576 and sampling rate is 44100Hz therefore maximum record time is 1048576/44100 = 24 seconds (approx.) If maximum record time is reached, recording will cease and the record button is deactivated. Playback can be either 'one shot' (triangle + line button) or looped (triangle button). The recorded data is monophonic and live input is read from the first (left) input channel but this could easily be modified to record in stereo (or more). Included are controls for playback speed and for playback start and end points. If the start and end points are inverted playback will be reversed.</label>
270 <alignment>left</alignment>
271 <font>Liberation Sans</font>
272 <fontsize>14</fontsize>
273 <precision>3</precision>
274 <color>
275 <r>0</r>
276 <g>0</g>
277 <b>0</b>
278 </color>
279 <bgcolor mode="nobackground">
280 <r>255</r>
281 <g>255</g>
282 <b>255</b>
283 </bgcolor>
284 <bordermode>noborder</bordermode>
285 <borderradius>1</borderradius>
286 <borderwidth>1</borderwidth>
287 </bsbObject>
288 <bsbObject version="2" type="BSBLabel">
289 <objectName/>
290 <x>8</x>
291 <y>112</y>
292 <width>100</width>
293 <height>30</height>
294 <uuid>{2c897206-0dbc-47b5-ba68-915d83839ab6}</uuid>
295 <visible>true</visible>
296 <midichan>0</midichan>
297 <midicc>0</midicc>
298 <label>Speed</label>
299 <alignment>left</alignment>
300 <font>Liberation Sans</font>
301 <fontsize>10</fontsize>
302 <precision>3</precision>
303 <color>
304 <r>0</r>
305 <g>0</g>
306 <b>0</b>
307 </color>
308 <bgcolor mode="nobackground">
309 <r>255</r>
310 <g>255</g>
311 <b>255</b>
312 </bgcolor>
313 <bordermode>noborder</bordermode>
314 <borderradius>1</borderradius>
315 <borderwidth>1</borderwidth>
316 </bsbObject>
317 <bsbObject version="2" type="BSBHSlider">
318 <objectName>Speed</objectName>
319 <x>8</x>
320 <y>93</y>
321 <width>500</width>
322 <height>27</height>
323 <uuid>{304e78d8-b320-453a-b28f-86b0ca872b86}</uuid>
324 <visible>true</visible>
325 <midichan>0</midichan>
326 <midicc>0</midicc>
327 <minimum>0.10000000</minimum>
328 <maximum>2.00000000</maximum>
329 <value>1.00060000</value>
330 <mode>lin</mode>
331 <mouseControl act="jump">continuous</mouseControl>
332 <resolution>-1.00000000</resolution>
333 <randomizable group="0">false</randomizable>
334 </bsbObject>
335 <bsbObject version="2" type="BSBDisplay">
336 <objectName>Speed</objectName>
337 <x>448</x>
338 <y>112</y>
339 <width>60</width>
340 <height>30</height>
341 <uuid>{94c7e961-7981-4d21-942f-80bcc647daf5}</uuid>
342 <visible>true</visible>
343 <midichan>0</midichan>
344 <midicc>0</midicc>
345 <label>1.001</label>
346 <alignment>right</alignment>
347 <font>Liberation Sans</font>
348 <fontsize>9</fontsize>
349 <precision>3</precision>
350 <color>
351 <r>0</r>
352 <g>0</g>
353 <b>0</b>
354 </color>
355 <bgcolor mode="nobackground">
356 <r>255</r>
357 <g>255</g>
358 <b>255</b>
359 </bgcolor>
360 <bordermode>noborder</bordermode>
361 <borderradius>1</borderradius>
362 <borderwidth>1</borderwidth>
363 </bsbObject>
364 <bsbObject version="2" type="BSBButton">
365 <objectName/>
366 <x>187</x>
367 <y>42</y>
368 <width>80</width>
369 <height>30</height>
370 <uuid>{6fe53ee7-bd5d-4064-80fb-2ea628a7c5ef}</uuid>
371 <visible>true</visible>
372 <midichan>0</midichan>
373 <midicc>0</midicc>
374 <type>event</type>
375 <pressedValue>1.00000000</pressedValue>
376 <stringvalue/>
377 <text> Play</text>
378 <image>/</image>
379 <eventLine>i 12 0 -1</eventLine>
380 <latch>true</latch>
381 <latched>false</latched>
382 </bsbObject>
383 <bsbObject version="2" type="BSBButton">
384 <objectName>PlayStop_B</objectName>
385 <x>279</x>
386 <y>42</y>
387 <width>82</width>
388 <height>30</height>
389 <uuid>{f0ca6175-5df0-4532-a841-7dc3313ac41d}</uuid>
390 <visible>true</visible>
391 <midichan>0</midichan>
392 <midicc>0</midicc>
393 <type>value</type>
394 <pressedValue>1.00000000</pressedValue>
395 <stringvalue/>
396 <text> Play Stop</text>
397 <image>/</image>
398 <eventLine/>
399 <latch>false</latch>
400 <latched>false</latched>
401 </bsbObject>
402 <bsbObject version="2" type="BSBController">
403 <objectName>LoopBeg</objectName>
404 <x>8</x>
405 <y>153</y>
406 <width>500</width>
407 <height>14</height>
408 <uuid>{8ecd7788-78c9-479b-8364-3c4fc256ea10}</uuid>
409 <visible>true</visible>
410 <midichan>0</midichan>
411 <midicc>0</midicc>
412 <objectName2/>
413 <xMin>0.00000000</xMin>
414 <xMax>1.00000000</xMax>
415 <yMin>0.00000000</yMin>
416 <yMax>1.00000000</yMax>
417 <xValue>0.20800000</xValue>
418 <yValue>0.00000000</yValue>
419 <type>fill</type>
420 <pointsize>1</pointsize>
421 <fadeSpeed>0.00000000</fadeSpeed>
422 <mouseControl act="press">jump</mouseControl>
423 <color>
424 <r>0</r>
425 <g>234</g>
426 <b>0</b>
427 </color>
428 <randomizable mode="both" group="0">false</randomizable>
429 <bgcolor>
430 <r>0</r>
431 <g>0</g>
432 <b>0</b>
433 </bgcolor>
434 </bsbObject>
435 <bsbObject version="2" type="BSBController">
436 <objectName>LoopEnd</objectName>
437 <x>8</x>
438 <y>176</y>
439 <width>500</width>
440 <height>14</height>
441 <uuid>{5aab0f2a-2456-4053-a07d-5e70c1c3d0ea}</uuid>
442 <visible>true</visible>
443 <midichan>0</midichan>
444 <midicc>0</midicc>
445 <objectName2/>
446 <xMin>0.00000000</xMin>
447 <xMax>1.00000000</xMax>
448 <yMin>0.00000000</yMin>
449 <yMax>1.00000000</yMax>
450 <xValue>0.87400000</xValue>
451 <yValue>0.00000000</yValue>
452 <type>llif</type>
453 <pointsize>1</pointsize>
454 <fadeSpeed>0.00000000</fadeSpeed>
455 <mouseControl act="press">jump</mouseControl>
456 <color>
457 <r>0</r>
458 <g>234</g>
459 <b>0</b>
460 </color>
461 <randomizable mode="both" group="0">false</randomizable>
462 <bgcolor>
463 <r>0</r>
464 <g>0</g>
465 <b>0</b>
466 </bgcolor>
467 </bsbObject>
468 <bsbObject version="2" type="BSBLabel">
469 <objectName/>
470 <x>203</x>
471 <y>189</y>
472 <width>100</width>
473 <height>30</height>
474 <uuid>{995aad6e-4f08-434c-ab9b-1936b51e2b3a}</uuid>
475 <visible>true</visible>
476 <midichan>0</midichan>
477 <midicc>0</midicc>
478 <label>Loop Points</label>
479 <alignment>left</alignment>
480 <font>Liberation Sans</font>
481 <fontsize>10</fontsize>
482 <precision>3</precision>
483 <color>
484 <r>0</r>
485 <g>0</g>
486 <b>0</b>
487 </color>
488 <bgcolor mode="nobackground">
489 <r>255</r>
490 <g>255</g>
491 <b>255</b>
492 </bgcolor>
493 <bordermode>noborder</bordermode>
494 <borderradius>1</borderradius>
495 <borderwidth>1</borderwidth>
496 </bsbObject>
497 <bsbObject version="2" type="BSBController">
498 <objectName/>
499 <x>15</x>
500 <y>52</y>
501 <width>10</width>
502 <height>10</height>
503 <uuid>{38085679-3d54-407a-ba33-8ccad19971ef}</uuid>
504 <visible>true</visible>
505 <midichan>0</midichan>
506 <midicc>0</midicc>
507 <objectName2>Record_L</objectName2>
508 <xMin>0.00000000</xMin>
509 <xMax>1.00000000</xMax>
510 <yMin>0.00000000</yMin>
511 <yMax>1.00000000</yMax>
512 <xValue>0.12389381</xValue>
513 <yValue>0.00000000</yValue>
514 <type>fill</type>
515 <pointsize>1</pointsize>
516 <fadeSpeed>0.00000000</fadeSpeed>
517 <mouseControl act="press">jump</mouseControl>
518 <color>
519 <r>0</r>
520 <g>234</g>
521 <b>0</b>
522 </color>
523 <randomizable mode="both" group="0">false</randomizable>
524 <bgcolor>
525 <r>0</r>
526 <g>0</g>
527 <b>0</b>
528 </bgcolor>
529 </bsbObject>
530 <bsbObject version="2" type="BSBController">
531 <objectName/>
532 <x>285</x>
533 <y>52</y>
534 <width>10</width>
535 <height>10</height>
536 <uuid>{5b3b05aa-35e7-4e25-ab6d-31fa61bf8b76}</uuid>
537 <visible>true</visible>
538 <midichan>0</midichan>
539 <midicc>0</midicc>
540 <objectName2>PlayStop_L</objectName2>
541 <xMin>0.00000000</xMin>
542 <xMax>1.00000000</xMax>
543 <yMin>0.00000000</yMin>
544 <yMax>1.00000000</yMax>
545 <xValue>0.12389381</xValue>
546 <yValue>0.00000000</yValue>
547 <type>fill</type>
548 <pointsize>1</pointsize>
549 <fadeSpeed>0.00000000</fadeSpeed>
550 <mouseControl act="press">jump</mouseControl>
551 <color>
552 <r>0</r>
553 <g>234</g>
554 <b>0</b>
555 </color>
556 <randomizable mode="both" group="0">false</randomizable>
557 <bgcolor>
558 <r>0</r>
559 <g>0</g>
560 <b>0</b>
561 </bgcolor>
562 </bsbObject>
563 </bsbPanel>
564 <bsbPresets>
565 </bsbPresets>