Debian Codegraph @

src/Examples/CsoundQt/Useful/SpectrumAnalyzer.csd

raw
1 <CsoundSynthesizer>
2 <CsOptions>
3 -odac
4 --messagelevel=134
5 </CsOptions>
6 <CsInstruments>
7
8 sr = 44100
9 ksmps = 64
10 nchnls = 4
11 0dbfs = 1
12 A4 = 442
13
14 chn_k "peak", 1 ; Channel to get peak information from graph
15
16 gi_fftSizes[] fillarray 2048, 4096, 8192, 16384
17 gk_fftSize init 4096
18 gk_peakFreq init 0
19 gk_peakGain init 0
20
21 gi_refreshRates[] fillarray 10,12,15,18,20,24,30
22 gk_refreshRate init 20
23
24
25 instr PlayPeak
26 ; play the detected peak frequency
27 kamp = ampdb(gk_peakGain)
28 apeak oscili kamp, sc_lag(gk_peakFreq, 0.1)
29 apeak *= linsegr:a(0, 0.1, 1, 0.1, 0)
30 outch 1, apeak
31 endin
32
33 instr Spectrum
34 k0 invalue "ch1"
35 k1 invalue "ch2"
36 k2 invalue "ch3"
37 k3 invalue "ch4"
38 kfftSizeIndex invalue "fftsize"
39 kplayPeak invalue "playpeak"
40 kfilterlow invalue "filterlow"
41 kinGain invalue "ingain"
42 gk_peakGain invalue "peakgain"
43 krefreshRateIdx invalue "refreshrateidx"
44
45 ; We need to use chnget to receive the peak information
46 kpeak chnget "peak"
47 khasPeak = kpeak > 0 ? 1:0
48 gk_peakFreq samphold kpeak, khasPeak
49
50 if kplayPeak == 1 && changed(khasPeak) == 1 then
51 if khasPeak == 1 then
52 schedulek("PlayPeak", 0, -1)
53 else
54 turnoff2("PlayPeak", 0, 1)
55 endif
56 endif
57
58 kfftSize = gi_fftSizes[kfftSizeIndex]
59 krefreshRate = gi_refreshRates[krefreshRateIdx]
60
61 if kfftSize != gk_fftSize || krefreshRate != gk_refreshRate then
62 gk_fftSize = kfftSize
63 gk_refreshRate = krefreshRate
64 reinit dispReset
65 endif
66
67 a0 inch 1
68 a1 inch 2
69 a2 init 0
70 a3 init 0
71 a0 *= k0
72 a1 *= k1
73 if nchnls > 2 then
74 a2 inch 3
75 a2 *= k2
76 if nchnls > 3 then
77 a3 inch 4
78 a3 *= k3
79 endif
80 endif
81
82 amix = sum(a0, a1, a2, a3)
83 amix *= ampdb(kinGain)
84
85 ; filter low frequencies from spectrum
86 if kfilterlow == 1 then
87 aspectrum pareq amix, 30, 0, 0.05, 1
88 else
89 aspectrum = amix
90 endif
91
92 denorm aspectrum
93
94 dispReset:
95 iperiod = 1/i(gk_refreshRate)
96 print iperiod
97 dispfft aspectrum, iperiod, i(gk_fftSize)
98
99 endin
100
101 instr PostInit
102 ; this needs to run at a point where the fft curve already
103 ; exists, which, depending on gk_fftSize, is, at the latest,
104 ; 16384 / sr (16384 / 44100 = 0.37...)
105 outvalue "spectrum", "@find fft aspectrum"
106 outvalue "spectrum", "@getPeak peak"
107 turnoff
108 endin
109
110
111 schedule "Spectrum", 0, -1
112 schedule "PostInit", 16384/sr + 0.01, -1
113
114 </CsInstruments>
115 <CsScore>
116
117 </CsScore>
118 </CsoundSynthesizer>
119
120
121
122
123
124
125 <bsbPanel>
126 <label>Widgets</label>
127 <objectName/>
128 <x>0</x>
129 <y>0</y>
130 <width>1010</width>
131 <height>576</height>
132 <visible>true</visible>
133 <uuid/>
134 <bgcolor mode="background">
135 <r>22</r>
136 <g>22</g>
137 <b>22</b>
138 </bgcolor>
139 <bsbObject type="BSBGraph" version="2">
140 <objectName>spectrum</objectName>
141 <x>10</x>
142 <y>9</y>
143 <width>1000</width>
144 <height>500</height>
145 <uuid>{ae3dee92-fc91-44c4-9b0f-b8656a7eb4f3}</uuid>
146 <visible>true</visible>
147 <midichan>0</midichan>
148 <midicc>-3</midicc>
149 <description>Spectrum - Press mouse to show/play peak</description>
150 <value>0</value>
151 <objectName2/>
152 <zoomx>2.00000000</zoomx>
153 <zoomy>1.00000000</zoomy>
154 <dispx>1.00000000</dispx>
155 <dispy>1.00000000</dispy>
156 <modex>lin</modex>
157 <modey>lin</modey>
158 <showSelector>false</showSelector>
159 <showGrid>true</showGrid>
160 <showTableInfo>false</showTableInfo>
161 <showScrollbars>false</showScrollbars>
162 <enableTables>false</enableTables>
163 <enableDisplays>true</enableDisplays>
164 <all>true</all>
165 </bsbObject>
166 <bsbObject type="BSBCheckBox" version="2">
167 <objectName>ch1</objectName>
168 <x>10</x>
169 <y>520</y>
170 <width>30</width>
171 <height>30</height>
172 <uuid>{5aae7b7e-875a-492f-89cb-dbb779039dbe}</uuid>
173 <visible>true</visible>
174 <midichan>0</midichan>
175 <midicc>-3</midicc>
176 <description>Enable Channel 1</description>
177 <selected>true</selected>
178 <label/>
179 <pressedValue>1</pressedValue>
180 <randomizable group="0">false</randomizable>
181 </bsbObject>
182 <bsbObject type="BSBCheckBox" version="2">
183 <objectName>ch2</objectName>
184 <x>40</x>
185 <y>520</y>
186 <width>30</width>
187 <height>30</height>
188 <uuid>{00c02e40-ab30-4c76-8737-0f1c375df75a}</uuid>
189 <visible>true</visible>
190 <midichan>0</midichan>
191 <midicc>-3</midicc>
192 <description>Enable Channel 2</description>
193 <selected>false</selected>
194 <label/>
195 <pressedValue>1</pressedValue>
196 <randomizable group="0">false</randomizable>
197 </bsbObject>
198 <bsbObject type="BSBCheckBox" version="2">
199 <objectName>ch3</objectName>
200 <x>70</x>
201 <y>520</y>
202 <width>30</width>
203 <height>30</height>
204 <uuid>{cb2805f4-c9ec-4a28-ba5c-459a2e3cec59}</uuid>
205 <visible>true</visible>
206 <midichan>0</midichan>
207 <midicc>-3</midicc>
208 <description>Enable Channel 3</description>
209 <selected>false</selected>
210 <label/>
211 <pressedValue>1</pressedValue>
212 <randomizable group="0">false</randomizable>
213 </bsbObject>
214 <bsbObject type="BSBCheckBox" version="2">
215 <objectName>ch4</objectName>
216 <x>100</x>
217 <y>520</y>
218 <width>30</width>
219 <height>30</height>
220 <uuid>{2192eed2-7818-45d5-8593-200cb766966c}</uuid>
221 <visible>true</visible>
222 <midichan>0</midichan>
223 <midicc>-3</midicc>
224 <description>Enable Channel 4</description>
225 <selected>false</selected>
226 <label/>
227 <pressedValue>1</pressedValue>
228 <randomizable group="0">false</randomizable>
229 </bsbObject>
230 <bsbObject type="BSBDropdown" version="2">
231 <objectName>fftsize</objectName>
232 <x>150</x>
233 <y>520</y>
234 <width>80</width>
235 <height>30</height>
236 <uuid>{e3a55dea-950a-4c43-a7ef-50f107379f17}</uuid>
237 <visible>true</visible>
238 <midichan>0</midichan>
239 <midicc>-3</midicc>
240 <description>FFT size</description>
241 <bsbDropdownItemList>
242 <bsbDropdownItem>
243 <name>2048</name>
244 <value>0</value>
245 <stringvalue/>
246 </bsbDropdownItem>
247 <bsbDropdownItem>
248 <name>4096</name>
249 <value>1</value>
250 <stringvalue/>
251 </bsbDropdownItem>
252 <bsbDropdownItem>
253 <name>8192</name>
254 <value>2</value>
255 <stringvalue/>
256 </bsbDropdownItem>
257 <bsbDropdownItem>
258 <name>16384</name>
259 <value>3</value>
260 <stringvalue/>
261 </bsbDropdownItem>
262 </bsbDropdownItemList>
263 <selectedIndex>2</selectedIndex>
264 <randomizable group="0">false</randomizable>
265 </bsbObject>
266 <bsbObject type="BSBLabel" version="2">
267 <objectName/>
268 <x>30</x>
269 <y>550</y>
270 <width>80</width>
271 <height>25</height>
272 <uuid>{342a2cbb-91b5-413b-9de1-aa82f1d74a0d}</uuid>
273 <visible>true</visible>
274 <midichan>0</midichan>
275 <midicc>-3</midicc>
276 <description/>
277 <label>Channels</label>
278 <alignment>center</alignment>
279 <valignment>top</valignment>
280 <font>Arial</font>
281 <fontsize>13</fontsize>
282 <precision>3</precision>
283 <color>
284 <r>239</r>
285 <g>240</g>
286 <b>241</b>
287 </color>
288 <bgcolor mode="nobackground">
289 <r>255</r>
290 <g>255</g>
291 <b>255</b>
292 </bgcolor>
293 <bordermode>false</bordermode>
294 <borderradius>1</borderradius>
295 <borderwidth>0</borderwidth>
296 </bsbObject>
297 <bsbObject type="BSBLabel" version="2">
298 <objectName/>
299 <x>145</x>
300 <y>550</y>
301 <width>80</width>
302 <height>25</height>
303 <uuid>{09805d02-0ae6-4f4c-9c39-6b7e61fb1356}</uuid>
304 <visible>true</visible>
305 <midichan>0</midichan>
306 <midicc>-3</midicc>
307 <description/>
308 <label>FFT Size</label>
309 <alignment>center</alignment>
310 <valignment>top</valignment>
311 <font>Arial</font>
312 <fontsize>13</fontsize>
313 <precision>3</precision>
314 <color>
315 <r>241</r>
316 <g>241</g>
317 <b>241</b>
318 </color>
319 <bgcolor mode="nobackground">
320 <r>255</r>
321 <g>255</g>
322 <b>255</b>
323 </bgcolor>
324 <bordermode>false</bordermode>
325 <borderradius>1</borderradius>
326 <borderwidth>0</borderwidth>
327 </bsbObject>
328 <bsbObject type="BSBCheckBox" version="2">
329 <objectName>playpeak</objectName>
330 <x>459</x>
331 <y>519</y>
332 <width>30</width>
333 <height>30</height>
334 <uuid>{f8803c6a-6fba-459f-a113-0e33c0ff19d1}</uuid>
335 <visible>true</visible>
336 <midichan>0</midichan>
337 <midicc>0</midicc>
338 <description>Play the peak frequency when the mouse is pressed</description>
339 <selected>true</selected>
340 <label/>
341 <pressedValue>1</pressedValue>
342 <randomizable group="0">false</randomizable>
343 </bsbObject>
344 <bsbObject type="BSBLabel" version="2">
345 <objectName/>
346 <x>435</x>
347 <y>550</y>
348 <width>80</width>
349 <height>25</height>
350 <uuid>{67e8666d-183c-491d-b465-61d7a1a218d5}</uuid>
351 <visible>true</visible>
352 <midichan>0</midichan>
353 <midicc>-3</midicc>
354 <description/>
355 <label>Play peak</label>
356 <alignment>center</alignment>
357 <valignment>top</valignment>
358 <font>Arial</font>
359 <fontsize>13</fontsize>
360 <precision>3</precision>
361 <color>
362 <r>239</r>
363 <g>240</g>
364 <b>241</b>
365 </color>
366 <bgcolor mode="nobackground">
367 <r>255</r>
368 <g>255</g>
369 <b>255</b>
370 </bgcolor>
371 <bordermode>false</bordermode>
372 <borderradius>1</borderradius>
373 <borderwidth>0</borderwidth>
374 </bsbObject>
375 <bsbObject type="BSBKnob" version="2">
376 <objectName>ingain</objectName>
377 <x>585</x>
378 <y>515</y>
379 <width>56</width>
380 <height>54</height>
381 <uuid>{ce2ca6ad-fa91-4b89-a2eb-7c092f529f22}</uuid>
382 <visible>true</visible>
383 <midichan>1</midichan>
384 <midicc>7</midicc>
385 <description>Input Gain (dB)</description>
386 <minimum>-24.00000000</minimum>
387 <maximum>24.00000000</maximum>
388 <value>0.00000000</value>
389 <mode>lin</mode>
390 <mouseControl act="">continuous</mouseControl>
391 <resolution>0.01000000</resolution>
392 <randomizable group="0">false</randomizable>
393 <color>
394 <r>245</r>
395 <g>124</g>
396 <b>0</b>
397 </color>
398 <textcolor>#f57c00</textcolor>
399 <border>0</border>
400 <borderColor>#000000</borderColor>
401 <showvalue>true</showvalue>
402 <flatstyle>true</flatstyle>
403 <integerMode>true</integerMode>
404 </bsbObject>
405 <bsbObject type="BSBLabel" version="2">
406 <objectName/>
407 <x>505</x>
408 <y>530</y>
409 <width>80</width>
410 <height>25</height>
411 <uuid>{8e80da70-b7b1-4f2d-935c-3e78f2730099}</uuid>
412 <visible>true</visible>
413 <midichan>0</midichan>
414 <midicc>-3</midicc>
415 <description/>
416 <label>Input Gain (dB)</label>
417 <alignment>right</alignment>
418 <valignment>top</valignment>
419 <font>Arial</font>
420 <fontsize>13</fontsize>
421 <precision>3</precision>
422 <color>
423 <r>239</r>
424 <g>240</g>
425 <b>241</b>
426 </color>
427 <bgcolor mode="nobackground">
428 <r>255</r>
429 <g>255</g>
430 <b>255</b>
431 </bgcolor>
432 <bordermode>false</bordermode>
433 <borderradius>1</borderradius>
434 <borderwidth>0</borderwidth>
435 </bsbObject>
436 <bsbObject type="BSBKnob" version="2">
437 <objectName>peakgain</objectName>
438 <x>720</x>
439 <y>515</y>
440 <width>56</width>
441 <height>54</height>
442 <uuid>{5951dec6-0e29-4eed-a7c9-3438955fabc4}</uuid>
443 <visible>true</visible>
444 <midichan>0</midichan>
445 <midicc>0</midicc>
446 <description>Input Gain (dB)</description>
447 <minimum>-80.00000000</minimum>
448 <maximum>0.00000000</maximum>
449 <value>-23.70400000</value>
450 <mode>lin</mode>
451 <mouseControl act="">continuous</mouseControl>
452 <resolution>0.01000000</resolution>
453 <randomizable group="0">false</randomizable>
454 <color>
455 <r>245</r>
456 <g>124</g>
457 <b>0</b>
458 </color>
459 <textcolor>#f57c00</textcolor>
460 <border>0</border>
461 <borderColor>#512900</borderColor>
462 <showvalue>true</showvalue>
463 <flatstyle>true</flatstyle>
464 <integerMode>true</integerMode>
465 </bsbObject>
466 <bsbObject type="BSBLabel" version="2">
467 <objectName/>
468 <x>640</x>
469 <y>530</y>
470 <width>80</width>
471 <height>25</height>
472 <uuid>{b1e91595-3134-4b62-943e-17e7f17b2058}</uuid>
473 <visible>true</visible>
474 <midichan>0</midichan>
475 <midicc>-3</midicc>
476 <description/>
477 <label>Peak Gain (dB)</label>
478 <alignment>right</alignment>
479 <valignment>top</valignment>
480 <font>Arial</font>
481 <fontsize>13</fontsize>
482 <precision>3</precision>
483 <color>
484 <r>239</r>
485 <g>240</g>
486 <b>241</b>
487 </color>
488 <bgcolor mode="nobackground">
489 <r>255</r>
490 <g>255</g>
491 <b>255</b>
492 </bgcolor>
493 <bordermode>false</bordermode>
494 <borderradius>1</borderradius>
495 <borderwidth>0</borderwidth>
496 </bsbObject>
497 <bsbObject type="BSBLabel" version="2">
498 <objectName/>
499 <x>340</x>
500 <y>550</y>
501 <width>100</width>
502 <height>26</height>
503 <uuid>{d9b44e66-bfec-4a49-ba48-e52e29d1e889}</uuid>
504 <visible>true</visible>
505 <midichan>0</midichan>
506 <midicc>-3</midicc>
507 <description/>
508 <label>Filter low freqs</label>
509 <alignment>center</alignment>
510 <valignment>top</valignment>
511 <font>Arial</font>
512 <fontsize>13</fontsize>
513 <precision>3</precision>
514 <color>
515 <r>239</r>
516 <g>240</g>
517 <b>241</b>
518 </color>
519 <bgcolor mode="nobackground">
520 <r>255</r>
521 <g>255</g>
522 <b>255</b>
523 </bgcolor>
524 <bordermode>false</bordermode>
525 <borderradius>1</borderradius>
526 <borderwidth>0</borderwidth>
527 </bsbObject>
528 <bsbObject type="BSBCheckBox" version="2">
529 <objectName>filterlow</objectName>
530 <x>375</x>
531 <y>520</y>
532 <width>30</width>
533 <height>30</height>
534 <uuid>{761ac268-c49b-4473-a0df-c045ea9fdaa4}</uuid>
535 <visible>true</visible>
536 <midichan>0</midichan>
537 <midicc>0</midicc>
538 <description>Filter low frequencies</description>
539 <selected>true</selected>
540 <label/>
541 <pressedValue>1</pressedValue>
542 <randomizable group="0">false</randomizable>
543 </bsbObject>
544 <bsbObject type="BSBDropdown" version="2">
545 <objectName>refreshrateidx</objectName>
546 <x>250</x>
547 <y>520</y>
548 <width>80</width>
549 <height>30</height>
550 <uuid>{6aab35af-5577-4c32-a245-d69c50b0dfcd}</uuid>
551 <visible>true</visible>
552 <midichan>0</midichan>
553 <midicc>0</midicc>
554 <description>Refresh Rate</description>
555 <bsbDropdownItemList>
556 <bsbDropdownItem>
557 <name>10</name>
558 <value>0</value>
559 <stringvalue/>
560 </bsbDropdownItem>
561 <bsbDropdownItem>
562 <name>12</name>
563 <value>1</value>
564 <stringvalue/>
565 </bsbDropdownItem>
566 <bsbDropdownItem>
567 <name>15</name>
568 <value>2</value>
569 <stringvalue/>
570 </bsbDropdownItem>
571 <bsbDropdownItem>
572 <name>18</name>
573 <value>3</value>
574 <stringvalue/>
575 </bsbDropdownItem>
576 <bsbDropdownItem>
577 <name>20</name>
578 <value>4</value>
579 <stringvalue/>
580 </bsbDropdownItem>
581 <bsbDropdownItem>
582 <name>24</name>
583 <value>5</value>
584 <stringvalue/>
585 </bsbDropdownItem>
586 <bsbDropdownItem>
587 <name>30</name>
588 <value>6</value>
589 <stringvalue/>
590 </bsbDropdownItem>
591 </bsbDropdownItemList>
592 <selectedIndex>4</selectedIndex>
593 <randomizable group="0">false</randomizable>
594 </bsbObject>
595 <bsbObject type="BSBLabel" version="2">
596 <objectName/>
597 <x>250</x>
598 <y>550</y>
599 <width>84</width>
600 <height>25</height>
601 <uuid>{432a9bd1-1587-40a7-a618-e256a453b4e4}</uuid>
602 <visible>true</visible>
603 <midichan>0</midichan>
604 <midicc>-3</midicc>
605 <description/>
606 <label>Refresh Rate</label>
607 <alignment>center</alignment>
608 <valignment>top</valignment>
609 <font>Arial</font>
610 <fontsize>13</fontsize>
611 <precision>3</precision>
612 <color>
613 <r>241</r>
614 <g>241</g>
615 <b>241</b>
616 </color>
617 <bgcolor mode="nobackground">
618 <r>255</r>
619 <g>255</g>
620 <b>255</b>
621 </bgcolor>
622 <bordermode>false</bordermode>
623 <borderradius>1</borderradius>
624 <borderwidth>0</borderwidth>
625 </bsbObject>
626 <bsbObject type="BSBLabel" version="2">
627 <objectName/>
628 <x>794</x>
629 <y>519</y>
630 <width>196</width>
631 <height>45</height>
632 <uuid>{13da7982-27d8-46ae-a425-3aecf9e1c34c}</uuid>
633 <visible>true</visible>
634 <midichan>0</midichan>
635 <midicc>-3</midicc>
636 <description/>
637 <label>Press mouse in spectrum display to show nearest peak</label>
638 <alignment>left</alignment>
639 <valignment>center</valignment>
640 <font>Liberation Sans</font>
641 <fontsize>13</fontsize>
642 <precision>3</precision>
643 <color>
644 <r>239</r>
645 <g>239</g>
646 <b>239</b>
647 </color>
648 <bgcolor mode="background">
649 <r>48</r>
650 <g>53</g>
651 <b>58</b>
652 </bgcolor>
653 <bordermode>false</bordermode>
654 <borderradius>3</borderradius>
655 <borderwidth>0</borderwidth>
656 </bsbObject>
657 </bsbPanel>
658 <bsbPresets>
659 </bsbPresets>