Debian Codegraph @

src/Examples/CsoundQt/Miscellaneous/Autotuner.csd

raw
1 <CsoundSynthesizer>
2 <CsOptions>
3 -b128 -B256
4 </CsOptions>
5 <CsInstruments>
6
7 sr = 44100
8 ksmps = 64
9 nchnls = 1
10
11 ; By Victor Lazzarini 2010
12 ; From an original idea by Brian Carty
13 ; Adapted for CsoundQt by Andres Cabrera
14
15 gktrack_opcode init 0
16 gktrans_method init 0
17 gkenable init 0
18 gklevel init 0
19 gkinterval1_on init 0
20 gkinterval1 init 0
21 gkinterval2 init 0
22 gkinterval2_on init 0
23
24 opcode PitchShifter, a, akkii
25 setksmps 1 ; kr=sr
26 asig,kpitch,kfdb,idel,iwin xin
27 kdelrate = (kpitch-1)/idel
28 avdel phasor -kdelrate ; 1 to 0
29 avdel2 phasor -kdelrate, 0.5 ; 1/2 buffer offset
30 afade tablei avdel, iwin, 1, 0, 1 ; crossfade windows
31 afade2 tablei avdel2,iwin, 1, 0, 1
32 adump delayr idel
33 atap1 deltapi avdel*idel ; variable delay taps
34 atap2 deltapi avdel2*idel
35 amix = atap1*afade + atap2*afade2 ; fade in/out the delay taps
36 delayw asig+amix*kfdb ; in+feedback signals
37 xout amix
38 endop
39
40 /**** autotune ***********************************/
41 /* aout Autotune asig,ism,ikey,ifn[,imeth] */
42 /* asig - input */
43 /* ism - smoothing time in secs */
44 /* ikey - key (0 = C,... ,11 = B */
45 /* ifn - table containing scale pitch classes (7) */
46 /* imeth - pitch track method: 0 - pitch (default) */
47 /* 1 - ptrack, 2 - pitchamdf */
48 /***************************************************/
49
50 opcode Autotune, a, aiiik
51
52 iwinsize = 1024
53 ibase = 440
54 ibasemidi = 69
55
56 asig,ism,itrans,ifn,km xin
57
58 if km == 0 then
59 kfr, kamp pitch asig,0.1,6.00,9.00,0
60 kfr = cpsoct(kfr)
61 elseif km == 1 then
62 kfr, kamp ptrack asig, 1024
63 else
64 kfr, kamp pitchamdf asig,130,1040
65 endif
66
67 if (kfr > 20) kgoto ok
68 kfr = 440
69 ok:
70
71 ktemp = 12 * (log(kfr / ibase) / log(2)) + ibasemidi
72 ktet = round(ktemp)
73
74 kpos init 0
75 itrans = 2
76 test:
77 knote table kpos, ifn ; get a pitch class from table
78 ktest = ktet % 12 ; note mod 12
79 knote = (knote+itrans) % 12 ; plus transpose interval mod 12
80 if ktest == knote kgoto next ; test if note matches pitch class + transposition
81 kpos = kpos + 1 ; increment table pos
82 if kpos >= 7 kgoto shift ; if more than or pitch class set we need to shift it
83 kgoto test ; loop back
84
85 shift:
86 if (ktemp >= ktet) kgoto plus
87 ktet = ktet - 1
88 kgoto next
89 plus:
90 ktet = ktet + 1
91
92 next:
93 kpos = 0
94 ktarget = ibase * (2 ^ ((ktet - ibasemidi) / 12))
95 kratio = ktarget/kfr
96 kratioport port kratio, ism, ibase
97
98 aout PitchShifter asig,kratioport,0,0.1,5
99
100 xout aout
101 endop
102
103 opcode Autotune2, a, aiiik
104 ifftsize = 512
105 iwtype = 1
106 ibase = 440
107 ibasemidi = 69
108 asig,ism,itrans,ifn,km xin
109 fsig pvsanal asig, ifftsize, ifftsize / 4, ifftsize, iwtype;
110
111 if km == 0 then
112 kfr, kamp pitch asig,0.1,6.00,9.00,0
113 kfr = cpsoct(kfr)
114 elseif km == 1 then
115 kfr, kamp ptrack asig, 1024
116 else
117 kfr, kamp pitchamdf asig,130,1040
118 endif
119
120 if (kfr > 20) kgoto ok
121 kfr = 440
122 ok:
123 ktemp = 12 * (log(kfr / ibase) / log(2)) + ibasemidi
124 kmidi = round(ktemp)
125
126 kpos init 0
127 itrans = 2
128 test:
129 knote table kpos, ifn ; get a pitch class
130 ktest = kmidi % 12 ; note mod 12
131 knote = (knote+itrans) % 12
132
133 if ktest == knote kgoto next ; test if note matches pitch class + transposition
134 kpos = kpos + 1 ; increment table pos
135 if kpos >= 7 kgoto shift ; if more than or pitch class set we need to shift it
136 kgoto test ; loop back
137 shift:
138 if (ktemp >= kmidi) kgoto plus
139 kmidi = kmidi - 1
140 kgoto next
141 plus:
142 kmidi = kmidi + 1
143 next:
144 kpos = 0
145 ktarget = ibase * (2 ^ ((kmidi - ibasemidi) / 12))
146
147 kratio = ktarget/kfr
148 kratioport port kratio, ism, ibase
149 fauto pvscale fsig, kratioport ; transpose it (optional param: formants)
150 aout pvsynth fauto
151 xout aout
152
153 endop
154
155 instr 1 ; Values from GUI
156 gktrack_opcode invalue "track_opcode"
157 gktrans_method invalue "trans_method"
158 gkenable invalue "enable"
159 gklevel invalue "level"
160 gkinterval1_on invalue "interval1_on"
161 gkinterval1 invalue "interval1"
162 gkinterval2 invalue "interval2"
163 gkinterval2_on invalue "interval2_on"
164 endin
165
166 instr 10 ; Audio I/0
167
168 ain inch 1
169
170 kinterv1 = 2^(gkinterval1/12)
171 kinterv2 = 2^(gkinterval2/12)
172
173 if (gkenable == 1) then
174 ; First add transposed copies
175 if (gkinterval1_on == 1) then
176 ain2 PitchShifter ain*0.5,kinterv1,0,0.1,5
177 else
178 clear ain2
179 endif
180 if (gkinterval2_on == 1) then
181 ain3 PitchShifter ain*0.5,kinterv2,0,0.1,5
182 else
183 clear ain3
184 endif
185 ; Then autotune them
186 if (gktrans_method == 0) then
187 aout Autotune ain,0.01,p4,p5, gktrack_opcode
188 aout2 Autotune ain2,0.01,p4,p5, gktrack_opcode
189 aout3 Autotune ain3,0.01,p4,p5, gktrack_opcode
190 elseif (gktrans_method == 1) then
191 aout Autotune2 ain,0.01,p4,p5, gktrack_opcode
192 aout2 Autotune2 ain2,0.01,p4,p5, gktrack_opcode
193 aout3 Autotune2 ain3,0.01,p4,p5, gktrack_opcode
194 else
195 clear aout, aout2, aout3
196 endif
197
198 out (aout+aout2+aout3)*0.5*gklevel
199 else
200 out ain*gklevel
201 endif
202
203 endin
204
205
206 </CsInstruments>
207 <CsScore>
208
209 f3 0 8 -2 0 2 4 5 7 9 11 12 ; major mode
210 f4 0 8 -2 0 2 3 5 7 8 10 12 ; minor mode
211 f5 0 16384 20 1
212
213 i 1 0 -1
214 ; key mode intvl1 intvl2(semitones)
215 i10 0 -1 0 3 -5 4 ; C major 6-4 harmonies
216
217 ; Use this instead for natural minor
218 ;i10 0 -1 0 4 -3 5 ; C minor (natural) 6-3 harmonies
219
220 e 3600
221 </CsScore>
222 </CsoundSynthesizer>
223 <CsoundSynthesizer>
224 <bsbPanel>
225 <label>Widgets</label>
226 <objectName/>
227 <x>808</x>
228 <y>350</y>
229 <width>403</width>
230 <height>354</height>
231 <visible>true</visible>
232 <uuid/>
233 <bgcolor mode="background">
234 <r>85</r>
235 <g>0</g>
236 <b>0</b>
237 </bgcolor>
238 <bsbObject type="BSBLabel" version="2">
239 <objectName/>
240 <x>28</x>
241 <y>180</y>
242 <width>163</width>
243 <height>60</height>
244 <uuid>{6f608c97-65f2-42f0-ab21-c4def5d22c3b}</uuid>
245 <visible>true</visible>
246 <midichan>0</midichan>
247 <midicc>-3</midicc>
248 <description/>
249 <label>Transposition Interval 1</label>
250 <alignment>center</alignment>
251 <valignment>top</valignment>
252 <font>Arial</font>
253 <fontsize>14</fontsize>
254 <precision>3</precision>
255 <color>
256 <r>255</r>
257 <g>255</g>
258 <b>255</b>
259 </color>
260 <bgcolor mode="nobackground">
261 <r>255</r>
262 <g>255</g>
263 <b>255</b>
264 </bgcolor>
265 <bordermode>border</bordermode>
266 <borderradius>1</borderradius>
267 <borderwidth>1</borderwidth>
268 </bsbObject>
269 <bsbObject type="BSBSpinBox" version="2">
270 <objectName>interval1</objectName>
271 <x>98</x>
272 <y>206</y>
273 <width>51</width>
274 <height>26</height>
275 <uuid>{2f9e69bd-1122-4669-8462-fb42ab1884c9}</uuid>
276 <visible>true</visible>
277 <midichan>0</midichan>
278 <midicc>-3</midicc>
279 <description/>
280 <alignment>left</alignment>
281 <font>Arial</font>
282 <fontsize>14</fontsize>
283 <color>
284 <r>0</r>
285 <g>0</g>
286 <b>0</b>
287 </color>
288 <bgcolor mode="nobackground">
289 <r>255</r>
290 <g>255</g>
291 <b>255</b>
292 </bgcolor>
293 <resolution>1.00000000</resolution>
294 <minimum>-12</minimum>
295 <maximum>12</maximum>
296 <randomizable group="0">false</randomizable>
297 <value>4</value>
298 </bsbObject>
299 <bsbObject type="BSBCheckBox" version="2">
300 <objectName>interval1_on</objectName>
301 <x>76</x>
302 <y>207</y>
303 <width>20</width>
304 <height>20</height>
305 <uuid>{5e3ac752-c4a9-41ed-9f18-d9c1967f0aee}</uuid>
306 <visible>true</visible>
307 <midichan>0</midichan>
308 <midicc>-3</midicc>
309 <description/>
310 <selected>true</selected>
311 <label/>
312 <pressedValue>1</pressedValue>
313 <randomizable group="0">false</randomizable>
314 </bsbObject>
315 <bsbObject type="BSBLabel" version="2">
316 <objectName/>
317 <x>29</x>
318 <y>244</y>
319 <width>132</width>
320 <height>28</height>
321 <uuid>{72b76ce1-8e70-46a3-91be-59471f9a3b35}</uuid>
322 <visible>true</visible>
323 <midichan>0</midichan>
324 <midicc>-3</midicc>
325 <description/>
326 <label>Output Level</label>
327 <alignment>right</alignment>
328 <valignment>top</valignment>
329 <font>Arial</font>
330 <fontsize>14</fontsize>
331 <precision>3</precision>
332 <color>
333 <r>255</r>
334 <g>255</g>
335 <b>255</b>
336 </color>
337 <bgcolor mode="nobackground">
338 <r>255</r>
339 <g>255</g>
340 <b>255</b>
341 </bgcolor>
342 <bordermode>noborder</bordermode>
343 <borderradius>1</borderradius>
344 <borderwidth>0</borderwidth>
345 </bsbObject>
346 <bsbObject type="BSBHSlider" version="2">
347 <objectName>level</objectName>
348 <x>35</x>
349 <y>265</y>
350 <width>323</width>
351 <height>28</height>
352 <uuid>{fc437687-d391-4e3d-86ef-2e34f89b8295}</uuid>
353 <visible>true</visible>
354 <midichan>0</midichan>
355 <midicc>-3</midicc>
356 <description/>
357 <minimum>0.00000000</minimum>
358 <maximum>1.00000000</maximum>
359 <value>0.47368421</value>
360 <mode>lin</mode>
361 <mouseControl act="jump">continuous</mouseControl>
362 <resolution>-1.00000000</resolution>
363 <randomizable group="0">false</randomizable>
364 </bsbObject>
365 <bsbObject type="BSBCheckBox" version="2">
366 <objectName>enable</objectName>
367 <x>202</x>
368 <y>150</y>
369 <width>20</width>
370 <height>20</height>
371 <uuid>{c8c56805-9a3c-426f-bc18-b4f06589d320}</uuid>
372 <visible>true</visible>
373 <midichan>0</midichan>
374 <midicc>-3</midicc>
375 <description/>
376 <selected>true</selected>
377 <label/>
378 <pressedValue>1</pressedValue>
379 <randomizable group="0">false</randomizable>
380 </bsbObject>
381 <bsbObject type="BSBLabel" version="2">
382 <objectName/>
383 <x>123</x>
384 <y>147</y>
385 <width>80</width>
386 <height>29</height>
387 <uuid>{c91c76ac-68c1-423e-b656-191c9c258dcd}</uuid>
388 <visible>true</visible>
389 <midichan>0</midichan>
390 <midicc>-3</midicc>
391 <description/>
392 <label>Enable</label>
393 <alignment>right</alignment>
394 <valignment>top</valignment>
395 <font>Arial</font>
396 <fontsize>14</fontsize>
397 <precision>3</precision>
398 <color>
399 <r>255</r>
400 <g>255</g>
401 <b>255</b>
402 </color>
403 <bgcolor mode="nobackground">
404 <r>255</r>
405 <g>255</g>
406 <b>255</b>
407 </bgcolor>
408 <bordermode>noborder</bordermode>
409 <borderradius>1</borderradius>
410 <borderwidth>0</borderwidth>
411 </bsbObject>
412 <bsbObject type="BSBButton" version="2">
413 <objectName>_Play</objectName>
414 <x>228</x>
415 <y>18</y>
416 <width>100</width>
417 <height>30</height>
418 <uuid>{37afd7a4-7c53-45e1-a40a-1f9204bb5437}</uuid>
419 <visible>true</visible>
420 <midichan>0</midichan>
421 <midicc>-3</midicc>
422 <description/>
423 <type>value</type>
424 <pressedValue>1.00000000</pressedValue>
425 <stringvalue/>
426 <text>Start</text>
427 <image>/</image>
428 <eventLine/>
429 <latch>false</latch>
430 <latched>false</latched>
431 <fontsize>10</fontsize>
432 </bsbObject>
433 <bsbObject type="BSBLabel" version="2">
434 <objectName/>
435 <x>56</x>
436 <y>12</y>
437 <width>166</width>
438 <height>50</height>
439 <uuid>{46a66da6-a7cd-44f2-b93f-39863e3b1f95}</uuid>
440 <visible>true</visible>
441 <midichan>0</midichan>
442 <midicc>-3</midicc>
443 <description/>
444 <label>Autotuner</label>
445 <alignment>right</alignment>
446 <valignment>top</valignment>
447 <font>Arial</font>
448 <fontsize>28</fontsize>
449 <precision>3</precision>
450 <color>
451 <r>255</r>
452 <g>255</g>
453 <b>255</b>
454 </color>
455 <bgcolor mode="nobackground">
456 <r>255</r>
457 <g>255</g>
458 <b>255</b>
459 </bgcolor>
460 <bordermode>border</bordermode>
461 <borderradius>3</borderradius>
462 <borderwidth>3</borderwidth>
463 </bsbObject>
464 <bsbObject type="BSBLabel" version="2">
465 <objectName/>
466 <x>58</x>
467 <y>9</y>
468 <width>166</width>
469 <height>50</height>
470 <uuid>{380efbe4-92b3-42c6-a98b-2b38b86ae2b6}</uuid>
471 <visible>true</visible>
472 <midichan>0</midichan>
473 <midicc>-3</midicc>
474 <description/>
475 <label>Autotuner</label>
476 <alignment>right</alignment>
477 <valignment>top</valignment>
478 <font>Arial</font>
479 <fontsize>28</fontsize>
480 <precision>3</precision>
481 <color>
482 <r>129</r>
483 <g>129</g>
484 <b>129</b>
485 </color>
486 <bgcolor mode="nobackground">
487 <r>255</r>
488 <g>255</g>
489 <b>255</b>
490 </bgcolor>
491 <bordermode>border</bordermode>
492 <borderradius>3</borderradius>
493 <borderwidth>4</borderwidth>
494 </bsbObject>
495 <bsbObject type="BSBLabel" version="2">
496 <objectName/>
497 <x>60</x>
498 <y>7</y>
499 <width>166</width>
500 <height>50</height>
501 <uuid>{22d03e4e-6d71-4926-8137-9bbbaf150e45}</uuid>
502 <visible>true</visible>
503 <midichan>0</midichan>
504 <midicc>-3</midicc>
505 <description/>
506 <label>Autotuner</label>
507 <alignment>right</alignment>
508 <valignment>top</valignment>
509 <font>Arial</font>
510 <fontsize>28</fontsize>
511 <precision>3</precision>
512 <color>
513 <r>34</r>
514 <g>34</g>
515 <b>34</b>
516 </color>
517 <bgcolor mode="nobackground">
518 <r>255</r>
519 <g>255</g>
520 <b>255</b>
521 </bgcolor>
522 <bordermode>border</bordermode>
523 <borderradius>3</borderradius>
524 <borderwidth>4</borderwidth>
525 </bsbObject>
526 <bsbObject type="BSBLabel" version="2">
527 <objectName/>
528 <x>34</x>
529 <y>109</y>
530 <width>162</width>
531 <height>31</height>
532 <uuid>{5cf105cc-f7d7-40d5-852d-9da0a0074908}</uuid>
533 <visible>true</visible>
534 <midichan>0</midichan>
535 <midicc>-3</midicc>
536 <description/>
537 <label>Transposition method</label>
538 <alignment>right</alignment>
539 <valignment>top</valignment>
540 <font>Arial</font>
541 <fontsize>14</fontsize>
542 <precision>3</precision>
543 <color>
544 <r>255</r>
545 <g>255</g>
546 <b>255</b>
547 </color>
548 <bgcolor mode="nobackground">
549 <r>255</r>
550 <g>255</g>
551 <b>255</b>
552 </bgcolor>
553 <bordermode>noborder</bordermode>
554 <borderradius>1</borderradius>
555 <borderwidth>0</borderwidth>
556 </bsbObject>
557 <bsbObject type="BSBDropdown" version="2">
558 <objectName>trans_method</objectName>
559 <x>197</x>
560 <y>108</y>
561 <width>158</width>
562 <height>31</height>
563 <uuid>{4b218834-c47b-4cbf-999a-0f372c7dc6d8}</uuid>
564 <visible>true</visible>
565 <midichan>0</midichan>
566 <midicc>-3</midicc>
567 <description/>
568 <bsbDropdownItemList>
569 <bsbDropdownItem>
570 <name>delay line</name>
571 <value>0</value>
572 <stringvalue/>
573 </bsbDropdownItem>
574 <bsbDropdownItem>
575 <name>spectral</name>
576 <value>1</value>
577 <stringvalue/>
578 </bsbDropdownItem>
579 </bsbDropdownItemList>
580 <selectedIndex>1</selectedIndex>
581 <randomizable group="0">false</randomizable>
582 </bsbObject>
583 <bsbObject type="BSBLabel" version="2">
584 <objectName/>
585 <x>33</x>
586 <y>73</y>
587 <width>162</width>
588 <height>31</height>
589 <uuid>{1983037d-7c0c-4259-b041-e068b74478f1}</uuid>
590 <visible>true</visible>
591 <midichan>0</midichan>
592 <midicc>-3</midicc>
593 <description/>
594 <label>Tracking opcode</label>
595 <alignment>right</alignment>
596 <valignment>top</valignment>
597 <font>Arial</font>
598 <fontsize>14</fontsize>
599 <precision>3</precision>
600 <color>
601 <r>255</r>
602 <g>255</g>
603 <b>255</b>
604 </color>
605 <bgcolor mode="nobackground">
606 <r>255</r>
607 <g>255</g>
608 <b>255</b>
609 </bgcolor>
610 <bordermode>noborder</bordermode>
611 <borderradius>1</borderradius>
612 <borderwidth>0</borderwidth>
613 </bsbObject>
614 <bsbObject type="BSBDropdown" version="2">
615 <objectName>track_opcode</objectName>
616 <x>196</x>
617 <y>72</y>
618 <width>158</width>
619 <height>31</height>
620 <uuid>{628026ef-23f9-4867-bc27-ca1362aae68f}</uuid>
621 <visible>true</visible>
622 <midichan>0</midichan>
623 <midicc>-3</midicc>
624 <description/>
625 <bsbDropdownItemList>
626 <bsbDropdownItem>
627 <name>pitch</name>
628 <value>0</value>
629 <stringvalue/>
630 </bsbDropdownItem>
631 <bsbDropdownItem>
632 <name>ptrack</name>
633 <value>1</value>
634 <stringvalue/>
635 </bsbDropdownItem>
636 <bsbDropdownItem>
637 <name>pitchamdf</name>
638 <value>2</value>
639 <stringvalue/>
640 </bsbDropdownItem>
641 </bsbDropdownItemList>
642 <selectedIndex>1</selectedIndex>
643 <randomizable group="0">false</randomizable>
644 </bsbObject>
645 <bsbObject type="BSBLabel" version="2">
646 <objectName/>
647 <x>195</x>
648 <y>180</y>
649 <width>163</width>
650 <height>60</height>
651 <uuid>{74d5d5df-cc44-47c3-bad8-ec01168c7c6e}</uuid>
652 <visible>true</visible>
653 <midichan>0</midichan>
654 <midicc>-3</midicc>
655 <description/>
656 <label>Transposition Interval 2</label>
657 <alignment>center</alignment>
658 <valignment>top</valignment>
659 <font>Arial</font>
660 <fontsize>14</fontsize>
661 <precision>3</precision>
662 <color>
663 <r>255</r>
664 <g>255</g>
665 <b>255</b>
666 </color>
667 <bgcolor mode="nobackground">
668 <r>255</r>
669 <g>255</g>
670 <b>255</b>
671 </bgcolor>
672 <bordermode>border</bordermode>
673 <borderradius>1</borderradius>
674 <borderwidth>1</borderwidth>
675 </bsbObject>
676 <bsbObject type="BSBSpinBox" version="2">
677 <objectName>interval2</objectName>
678 <x>265</x>
679 <y>206</y>
680 <width>51</width>
681 <height>26</height>
682 <uuid>{9aee3b1f-c20d-42a2-9f93-20767d82cba7}</uuid>
683 <visible>true</visible>
684 <midichan>0</midichan>
685 <midicc>-3</midicc>
686 <description/>
687 <alignment>left</alignment>
688 <font>Arial</font>
689 <fontsize>14</fontsize>
690 <color>
691 <r>0</r>
692 <g>0</g>
693 <b>0</b>
694 </color>
695 <bgcolor mode="nobackground">
696 <r>255</r>
697 <g>255</g>
698 <b>255</b>
699 </bgcolor>
700 <resolution>1.00000000</resolution>
701 <minimum>-12</minimum>
702 <maximum>12</maximum>
703 <randomizable group="0">false</randomizable>
704 <value>7</value>
705 </bsbObject>
706 <bsbObject type="BSBCheckBox" version="2">
707 <objectName>interval2_on</objectName>
708 <x>243</x>
709 <y>207</y>
710 <width>20</width>
711 <height>20</height>
712 <uuid>{4a6d95d4-34a5-410e-8f93-c05ee7a0797e}</uuid>
713 <visible>true</visible>
714 <midichan>0</midichan>
715 <midicc>-3</midicc>
716 <description/>
717 <selected>true</selected>
718 <label/>
719 <pressedValue>1</pressedValue>
720 <randomizable group="0">false</randomizable>
721 </bsbObject>
722 </bsbPanel>
723 <bsbPresets>
724 </bsbPresets>