1<CsoundSynthesizer>
2<CsOptions>
3--env:SSDIR+=../SourceMaterials -i adc -o dac -d -m0
4</CsOptions>
5<CsInstruments>
6;example written by Joachim Heintz
7sr = 44100
8ksmps = 32
9nchnls = 1
100dbfs = 1
11
12  opcode BufCrt1, i, io
13ilen, inum xin
14ift       ftgen     inum, 0, -(ilen*sr), 2, 0
15          xout      ift
16  endop
17
18  opcode BufRec1, 0, aik
19ain, ift, krec  xin
20          setksmps  1
21imaxindx  =         ftlen(ift)-1 ;max index to write
22knew      changed   krec
23if krec == 1 then ;record as long as krec=1
24 if knew == 1 then ;reset index if restarted
25kndx      =         0
26 endif
27kndx      =         (kndx > imaxindx ? imaxindx : kndx)
28andx      =         kndx
29          tabw      ain, andx, ift
30kndx      =         kndx+1
31endif
32  endop
33
34  opcode BufPlay1, a, ik
35ift, kplay  xin
36          setksmps  1
37imaxindx  =         ftlen(ift)-1 ;max index to read
38knew      changed   kplay
39if kplay == 1 then ;play as long as kplay=1
40 if knew == 1 then ;reset index if restarted
41kndx      =         0
42 endif
43kndx      =         (kndx > imaxindx ? imaxindx : kndx)
44andx      =         kndx
45aout      tab       andx, ift
46kndx      =         kndx+1
47endif
48          xout      aout
49  endop
50
51  opcode KeyStay, k, kkk
52;returns 1 as long as a certain key is pressed
53key, k0, kascii    xin ;ascii code of the key (e.g. 32 for space)
54kprev     init      0 ;previous key value
55kout      =         (key == kascii || (key == -1 && kprev == kascii) ? 1 : 0)
56kprev     =         (key > 0 ? key : kprev)
57kprev     =         (kprev == key && k0 == 0 ? 0 : kprev)
58          xout      kout
59  endop
60
61  opcode KeyStay2, kk, kk
62;combines two KeyStay UDO's (this way is necessary
63;because just one sensekey opcode is possible in an orchestra)
64kasci1, kasci2 xin ;two ascii codes as input
65key,k0    sensekey
66kout1     KeyStay   key, k0, kasci1
67kout2     KeyStay   key, k0, kasci2
68          xout      kout1, kout2
69  endop
70
71
72instr 1
73ain        inch      1 ;audio input on channel 1
74iBuf       BufCrt1   3 ;buffer for 3 seconds of recording
75kRec,kPlay KeyStay2  114, 112 ;define keys for record and play
76           BufRec1   ain, iBuf, kRec ;record if kRec=1
77aout       BufPlay1  iBuf, kPlay ;play if kPlay=1
78           out       aout ;send out
79endin
80
81</CsInstruments>
82<CsScore>
83i 1 0 1000
84</CsScore>
85</CsoundSynthesizer>
86