Debian Codegraph @

src/Examples/CsoundQt/Useful/Envelope_Extractor.csd

raw
1 <CsoundSynthesizer>
2 <CsOptions>
3 -+max_str_len=10000 -n
4 </CsOptions>
5 <CsInstruments>
6
7 /***Envelope Extractor***/
8 ;example for CsoundQt
9 ;joachim heintz mar 2011
10
11 sr = 44100
12 ksmps = 1 ;use ksmps=1 for avoiding roundoff inaccuracies
13 nchnls = 1
14 0dbfs = 1
15
16
17 opcode StrayGetEl, ii, Sijj
18 ;returns the startindex and the endindex (= the first space after the element) for ielindex in String. if startindex returns -1, the element has not been found
19 Stray, ielindx, isepA, isepB xin
20 ;;DEFINE THE SEPARATORS
21 isep1 = (isepA == -1 ? 32 : isepA)
22 isep2 = (isepA == -1 && isepB == -1 ? 9 : (isepB == -1 ? isep1 : isepB))
23 Sep1 sprintf "%c", isep1
24 Sep2 sprintf "%c", isep2
25 ;;INITIALIZE SOME PARAMETERS
26 ilen strlen Stray
27 istartsel = -1; startindex for searched element
28 iendsel = -1; endindex for searched element
29 iel = 0; actual number of element while searching
30 iwarleer = 1
31 indx = 0
32 if ilen == 0 igoto end ;don't go into the loop if Stray is empty
33 loop:
34 Snext strsub Stray, indx, indx+1; next sign
35 isep1p strcmp Snext, Sep1; returns 0 if Snext is sep1
36 isep2p strcmp Snext, Sep2; 0 if Snext is sep2
37 ;;NEXT SIGN IS NOT SEP1 NOR SEP2
38 if isep1p != 0 && isep2p != 0 then
39 if iwarleer == 1 then; first character after a separator
40 if iel == ielindx then; if searched element index
41 istartsel = indx; set it
42 iwarleer = 0
43 else ;if not searched element index
44 iel = iel+1; increase it
45 iwarleer = 0; log that it's not a separator
46 endif
47 endif
48 ;;NEXT SIGN IS SEP1 OR SEP2
49 else
50 if istartsel > -1 then; if this is first selector after searched element
51 iendsel = indx; set iendsel
52 igoto end ;break
53 else
54 iwarleer = 1
55 endif
56 endif
57 loop_lt indx, 1, ilen, loop
58 end: xout istartsel, iendsel
59 endop
60
61 opcode StrayLen, i, Sjj
62 ;returns the number of elements in Stray. elements are defined by two separators as ASCII coded characters: isep1 defaults to 32 (= space), isep2 defaults to 9 (= tab). if just one separator is used, isep2 equals isep1
63 Stray, isepA, isepB xin
64 ;;DEFINE THE SEPARATORS
65 isep1 = (isepA == -1 ? 32 : isepA)
66 isep2 = (isepA == -1 && isepB == -1 ? 9 : (isepB == -1 ? isep1 : isepB))
67 Sep1 sprintf "%c", isep1
68 Sep2 sprintf "%c", isep2
69 ;;INITIALIZE SOME PARAMETERS
70 ilen strlen Stray
71 icount = 0; number of elements
72 iwarsep = 1
73 indx = 0
74 if ilen == 0 igoto end ;don't go into the loop if String is empty
75 loop:
76 Snext strsub Stray, indx, indx+1; next sign
77 isep1p strcmp Snext, Sep1; returns 0 if Snext is sep1
78 isep2p strcmp Snext, Sep2; 0 if Snext is sep2
79 if isep1p == 0 || isep2p == 0 then; if sep1 or sep2
80 iwarsep = 1; tell the log so
81 else ; if not
82 if iwarsep == 1 then ; and has been sep1 or sep2 before
83 icount = icount + 1; increase counter
84 iwarsep = 0; and tell you are ot sep1 nor sep2
85 endif
86 endif
87 loop_lt indx, 1, ilen, loop
88 end: xout icount
89 endop
90
91 opcode StrayGetNum, i, Sijj
92 ;returns the startindex and the endindex (= the first space after the element) for ielindex in String. if startindex returns -1, the element has not been found
93 Stray, ielindx, isepA, isepB xin
94 ;;DEFINE THE SEPARATORS
95 isep1 = (isepA == -1 ? 32 : isepA)
96 isep2 = (isepA == -1 && isepB == -1 ? 9 : (isepB == -1 ? isep1 : isepB))
97 Sep1 sprintf "%c", isep1
98 Sep2 sprintf "%c", isep2
99 ;;INITIALIZE SOME PARAMETERS
100 ilen strlen Stray
101 istartsel = -1; startindex for searched element
102 iendsel = -1; endindex for searched element
103 iel = 0; actual number of element while searching
104 iwarleer = 1
105 indx = 0
106 if ilen == 0 igoto end ;don't go into the loop if Stray is empty
107 loop:
108 Snext strsub Stray, indx, indx+1; next sign
109 isep1p strcmp Snext, Sep1; returns 0 if Snext is sep1
110 isep2p strcmp Snext, Sep2; 0 if Snext is sep2
111 ;;NEXT SIGN IS NOT SEP1 NOR SEP2
112 if isep1p != 0 && isep2p != 0 then
113 if iwarleer == 1 then; first character after a separator
114 if iel == ielindx then; if searched element index
115 istartsel = indx; set it
116 iwarleer = 0
117 else ;if not searched element index
118 iel = iel+1; increase it
119 iwarleer = 0; log that it's not a separator
120 endif
121 endif
122 ;;NEXT SIGN IS SEP1 OR SEP2
123 else
124 if istartsel > -1 then; if this is first selector after searched element
125 iendsel = indx; set iendsel
126 igoto end ;break
127 else
128 iwarleer = 1
129 endif
130 endif
131 loop_lt indx, 1, ilen, loop
132 end:
133 Snum strsub Stray, istartsel, iendsel
134 inum strtod Snum
135 xout inum
136 endop
137
138 opcode FilePlay1, a, Skoo
139 ;gives mono output regardless your soundfile is mono or stereo
140 ;(if stereo, just the first channel is used)
141 Sfil, kspeed, iskip, iloop xin
142 ichn filenchnls Sfil
143 if ichn == 1 then
144 aout diskin2 Sfil, kspeed, iskip, iloop
145 else
146 aout, ano diskin2 Sfil, kspeed, iskip, iloop
147 endif
148 xout aout
149 endop
150
151
152 instr 1 ;distribute values and calls instr 2
153 Sfiles invalue "_MBrowse"
154 Snpoints invalue "npoints"
155 Sftnums invalue "ftnums"
156 gSoutfil invalue "_Browse"
157 inumfils StrayLen Sfiles, 124
158 p3 = inumfils/10 + 0.1
159 inumpnts StrayLen Snpoints
160 inumftns StrayLen Sftnums
161 indx = 0
162 loop:
163 ist, ien StrayGetEl Sfiles, indx, 124
164 Sfile strsub Sfiles, ist, ien
165 if inumpnts == 1 then
166 inpoints StrayGetNum Snpoints, 0
167 else
168 inpoints StrayGetNum Snpoints, indx
169 endif
170 if inumftns == 1 then
171 iftnum StrayGetNum Sftnums, 0
172 else
173 iftnum StrayGetNum Sftnums, indx
174 endif
175 Scall sprintf {{i 2 %f .1 "%s" %d %d}}, indx/10, Sfile, inpoints, iftnum
176 scoreline_i Scall
177 loop_lt indx, 1, inumfils, loop
178 endin
179
180 instr 2 ;performs analysis and calls instr 3
181 Sfile strget p4
182 inpoints = p5
183 iftnum = p6
184
185 ilslash strrindex Sfile, "/"
186 Snamsuf strsub Sfile, ilslash+1
187 ilsuf strrindex Snamsuf, "."
188 Sname strsub Snamsuf, 0, ilsuf
189
190 Sformat sprintf "gi_%s ftgen %d, 0, -%d, -2", Sname, iftnum, inpoints
191 ;fprints gSoutfil, Sformat
192
193 ifilen filelen Sfile
194 ifilkcyc = ifilen*sr/ksmps ;number of k-cycles
195
196 kcount init 0
197 loop:
198 idt = ifilen / inpoints
199 asig FilePlay1 Sfile, 1
200 aenv follow asig, idt
201 kenv downsamp aenv
202 if kcount % round(ifilkcyc/inpoints) == 0 then
203 Snew sprintfk "%s, %f", Sformat, kenv
204 Sformat strcpyk Snew
205 endif
206 loop_lt kcount, 1, ifilkcyc, loop
207 gSwrite strcatk Sformat, "\n"
208 event "i", 3, 0, .1
209 turnoff
210 endin
211
212 instr 3 ;writes resulting tables to file
213 fprints gSoutfil, gSwrite
214 printf_i "Values written to file '%s'\n", 1, gSoutfil
215 endin
216
217 </CsInstruments>
218 <CsScore>
219 i 1 0 1
220 </CsScore>
221 </CsoundSynthesizer>
222
223 <bsbPanel>
224 <label>Widgets</label>
225 <objectName/>
226 <x>0</x>
227 <y>0</y>
228 <width>788</width>
229 <height>466</height>
230 <visible>true</visible>
231 <uuid/>
232 <bgcolor mode="background">
233 <r>170</r>
234 <g>170</g>
235 <b>127</b>
236 </bgcolor>
237 <bsbObject version="2" type="BSBLineEdit">
238 <objectName>_MBrowse</objectName>
239 <x>274</x>
240 <y>261</y>
241 <width>343</width>
242 <height>29</height>
243 <uuid>{fe1e66fe-4105-4aed-9c04-e06ef3c95aa2}</uuid>
244 <visible>true</visible>
245 <midichan>0</midichan>
246 <midicc>0</midicc>
247 <label>/home/jh/Joachim/Materialien/SamplesKlangbearbeitung/BratscheMonoOneCycle.wav|/home/jh/Joachim/Materialien/SamplesKlangbearbeitung/EineWelleMono.aiff|/home/jh/Joachim/Materialien/SamplesKlangbearbeitung/Glocke_Ganze1.aiff|/home/jh/Joachim/Materialien/SamplesKlangbearbeitung/Kontrabass.aif</label>
248 <alignment>right</alignment>
249 <font>DejaVu Sans</font>
250 <fontsize>14</fontsize>
251 <precision>3</precision>
252 <color>
253 <r>0</r>
254 <g>0</g>
255 <b>0</b>
256 </color>
257 <bgcolor mode="nobackground">
258 <r>206</r>
259 <g>206</g>
260 <b>206</b>
261 </bgcolor>
262 <background>nobackground</background>
263 </bsbObject>
264 <bsbObject version="2" type="BSBButton">
265 <objectName>_MBrowse</objectName>
266 <x>621</x>
267 <y>260</y>
268 <width>100</width>
269 <height>30</height>
270 <uuid>{7e9d3773-29f5-4906-91f7-3e35e562dfa6}</uuid>
271 <visible>true</visible>
272 <midichan>0</midichan>
273 <midicc>0</midicc>
274 <type>value</type>
275 <pressedValue>1.00000000</pressedValue>
276 <stringvalue>/home/jh/Joachim/Materialien/SamplesKlangbearbeitung/BratscheMonoOneCycle.wav|/home/jh/Joachim/Materialien/SamplesKlangbearbeitung/EineWelleMono.aiff|/home/jh/Joachim/Materialien/SamplesKlangbearbeitung/Glocke_Ganze1.aiff|/home/jh/Joachim/Materialien/SamplesKlangbearbeitung/Kontrabass.aif</stringvalue>
277 <text>Select Files</text>
278 <image>/</image>
279 <eventLine/>
280 <latch>false</latch>
281 <latched>false</latched>
282 </bsbObject>
283 <bsbObject version="2" type="BSBLineEdit">
284 <objectName>npoints</objectName>
285 <x>274</x>
286 <y>332</y>
287 <width>343</width>
288 <height>29</height>
289 <uuid>{56ac7a23-6421-41d3-bede-c48a63166f2f}</uuid>
290 <visible>true</visible>
291 <midichan>0</midichan>
292 <midicc>0</midicc>
293 <label>100</label>
294 <alignment>left</alignment>
295 <font>Arial</font>
296 <fontsize>14</fontsize>
297 <precision>3</precision>
298 <color>
299 <r>0</r>
300 <g>0</g>
301 <b>0</b>
302 </color>
303 <bgcolor mode="nobackground">
304 <r>206</r>
305 <g>206</g>
306 <b>206</b>
307 </bgcolor>
308 <background>nobackground</background>
309 </bsbObject>
310 <bsbObject version="2" type="BSBLabel">
311 <objectName/>
312 <x>85</x>
313 <y>260</y>
314 <width>188</width>
315 <height>30</height>
316 <uuid>{c0c2d141-f138-41bc-8c76-f59252e79007}</uuid>
317 <visible>true</visible>
318 <midichan>0</midichan>
319 <midicc>0</midicc>
320 <label>Files to be analyzed</label>
321 <alignment>right</alignment>
322 <font>Arial</font>
323 <fontsize>14</fontsize>
324 <precision>3</precision>
325 <color>
326 <r>0</r>
327 <g>0</g>
328 <b>0</b>
329 </color>
330 <bgcolor mode="nobackground">
331 <r>255</r>
332 <g>255</g>
333 <b>255</b>
334 </bgcolor>
335 <bordermode>noborder</bordermode>
336 <borderradius>1</borderradius>
337 <borderwidth>1</borderwidth>
338 </bsbObject>
339 <bsbObject version="2" type="BSBLabel">
340 <objectName/>
341 <x>34</x>
342 <y>326</y>
343 <width>239</width>
344 <height>43</height>
345 <uuid>{59338b2d-457d-4151-9067-70221d3660d1}</uuid>
346 <visible>true</visible>
347 <midichan>0</midichan>
348 <midicc>0</midicc>
349 <label>Number of points in the
350 function tables to be created</label>
351 <alignment>right</alignment>
352 <font>Arial</font>
353 <fontsize>14</fontsize>
354 <precision>3</precision>
355 <color>
356 <r>0</r>
357 <g>0</g>
358 <b>0</b>
359 </color>
360 <bgcolor mode="nobackground">
361 <r>255</r>
362 <g>255</g>
363 <b>255</b>
364 </bgcolor>
365 <bordermode>noborder</bordermode>
366 <borderradius>1</borderradius>
367 <borderwidth>1</borderwidth>
368 </bsbObject>
369 <bsbObject version="2" type="BSBLineEdit">
370 <objectName>ftnums</objectName>
371 <x>274</x>
372 <y>375</y>
373 <width>343</width>
374 <height>29</height>
375 <uuid>{efef5429-8ce4-477c-b240-24a2627c6e99}</uuid>
376 <visible>true</visible>
377 <midichan>0</midichan>
378 <midicc>0</midicc>
379 <label>4000 4001 4002 4003</label>
380 <alignment>left</alignment>
381 <font>Arial</font>
382 <fontsize>14</fontsize>
383 <precision>3</precision>
384 <color>
385 <r>0</r>
386 <g>0</g>
387 <b>0</b>
388 </color>
389 <bgcolor mode="nobackground">
390 <r>206</r>
391 <g>206</g>
392 <b>206</b>
393 </bgcolor>
394 <background>nobackground</background>
395 </bsbObject>
396 <bsbObject version="2" type="BSBLabel">
397 <objectName/>
398 <x>33</x>
399 <y>371</y>
400 <width>240</width>
401 <height>41</height>
402 <uuid>{25982a05-2dd6-489a-9fbc-cc5d94250b4a}</uuid>
403 <visible>true</visible>
404 <midichan>0</midichan>
405 <midicc>0</midicc>
406 <label>identification number of the
407 function tables to be created</label>
408 <alignment>right</alignment>
409 <font>Arial</font>
410 <fontsize>14</fontsize>
411 <precision>3</precision>
412 <color>
413 <r>0</r>
414 <g>0</g>
415 <b>0</b>
416 </color>
417 <bgcolor mode="nobackground">
418 <r>255</r>
419 <g>255</g>
420 <b>255</b>
421 </bgcolor>
422 <bordermode>noborder</bordermode>
423 <borderradius>1</borderradius>
424 <borderwidth>1</borderwidth>
425 </bsbObject>
426 <bsbObject version="2" type="BSBLineEdit">
427 <objectName>_Browse</objectName>
428 <x>274</x>
429 <y>295</y>
430 <width>343</width>
431 <height>29</height>
432 <uuid>{37827e7b-547c-44ee-9ed7-91da451a1bc6}</uuid>
433 <visible>true</visible>
434 <midichan>0</midichan>
435 <midicc>0</midicc>
436 <label>/home/jh/env_out.txt</label>
437 <alignment>right</alignment>
438 <font>Arial</font>
439 <fontsize>14</fontsize>
440 <precision>3</precision>
441 <color>
442 <r>0</r>
443 <g>0</g>
444 <b>0</b>
445 </color>
446 <bgcolor mode="nobackground">
447 <r>206</r>
448 <g>206</g>
449 <b>206</b>
450 </bgcolor>
451 <background>nobackground</background>
452 </bsbObject>
453 <bsbObject version="2" type="BSBButton">
454 <objectName>_Browse</objectName>
455 <x>621</x>
456 <y>294</y>
457 <width>100</width>
458 <height>30</height>
459 <uuid>{9a15fa00-1fac-478d-9857-be9b89935695}</uuid>
460 <visible>true</visible>
461 <midichan>0</midichan>
462 <midicc>0</midicc>
463 <type>value</type>
464 <pressedValue>1.00000000</pressedValue>
465 <stringvalue>/home/jh/env_out.txt</stringvalue>
466 <text>Select File</text>
467 <image>/</image>
468 <eventLine/>
469 <latch>false</latch>
470 <latched>false</latched>
471 </bsbObject>
472 <bsbObject version="2" type="BSBLabel">
473 <objectName/>
474 <x>32</x>
475 <y>295</y>
476 <width>241</width>
477 <height>29</height>
478 <uuid>{5b54c68d-0de1-4915-9029-101af74e6f8d}</uuid>
479 <visible>true</visible>
480 <midichan>0</midichan>
481 <midicc>0</midicc>
482 <label>Text file to be written as result</label>
483 <alignment>right</alignment>
484 <font>Arial</font>
485 <fontsize>14</fontsize>
486 <precision>3</precision>
487 <color>
488 <r>0</r>
489 <g>0</g>
490 <b>0</b>
491 </color>
492 <bgcolor mode="nobackground">
493 <r>255</r>
494 <g>255</g>
495 <b>255</b>
496 </bgcolor>
497 <bordermode>noborder</bordermode>
498 <borderradius>1</borderradius>
499 <borderwidth>1</borderwidth>
500 </bsbObject>
501 <bsbObject version="2" type="BSBLabel">
502 <objectName/>
503 <x>21</x>
504 <y>7</y>
505 <width>699</width>
506 <height>56</height>
507 <uuid>{0fe8443c-9a6f-4f4a-9ea3-296c28ad4fb7}</uuid>
508 <visible>true</visible>
509 <midichan>0</midichan>
510 <midicc>0</midicc>
511 <label>ENVELOPE EXTRACTOR</label>
512 <alignment>center</alignment>
513 <font>Arial</font>
514 <fontsize>40</fontsize>
515 <precision>3</precision>
516 <color>
517 <r>0</r>
518 <g>0</g>
519 <b>0</b>
520 </color>
521 <bgcolor mode="nobackground">
522 <r>255</r>
523 <g>255</g>
524 <b>255</b>
525 </bgcolor>
526 <bordermode>noborder</bordermode>
527 <borderradius>1</borderradius>
528 <borderwidth>1</borderwidth>
529 </bsbObject>
530 <bsbObject version="2" type="BSBLabel">
531 <objectName/>
532 <x>20</x>
533 <y>69</y>
534 <width>700</width>
535 <height>187</height>
536 <uuid>{f02e3ae5-27d7-4d40-b5c2-242497d54eba}</uuid>
537 <visible>true</visible>
538 <midichan>0</midichan>
539 <midicc>0</midicc>
540 <label>This instrument analyses the envelope(s) of one or more given sample(s) and returns the result as function tables in this format:
541 gi_'filename' ftgen 'ftnum', 0, 'points', -2, val1, val2, ...
542 The number of points determines the resolution of your analysis. If the duration of your sample is two seconds, and you write a table of size 100 points, you will get the mean amplitude for each 2/100 = 0.02 seconds.
543 Select one or some sound files for analyzing, and a text file for writing the result to. Enter the desired number of points for the analysis, and the identification number of the table (for both either one number which is applied to all input files, or one number for each file).</label>
544 <alignment>left</alignment>
545 <font>Arial</font>
546 <fontsize>16</fontsize>
547 <precision>3</precision>
548 <color>
549 <r>0</r>
550 <g>0</g>
551 <b>0</b>
552 </color>
553 <bgcolor mode="nobackground">
554 <r>255</r>
555 <g>255</g>
556 <b>255</b>
557 </bgcolor>
558 <bordermode>noborder</bordermode>
559 <borderradius>1</borderradius>
560 <borderwidth>1</borderwidth>
561 </bsbObject>
562 <bsbObject version="2" type="BSBButton">
563 <objectName>_Render</objectName>
564 <x>621</x>
565 <y>331</y>
566 <width>100</width>
567 <height>74</height>
568 <uuid>{fa20b460-4974-43cd-9729-9bde68d583db}</uuid>
569 <visible>true</visible>
570 <midichan>0</midichan>
571 <midicc>0</midicc>
572 <type>value</type>
573 <pressedValue>1.00000000</pressedValue>
574 <stringvalue>/home/linux/Desktop/test2.txt</stringvalue>
575 <text>Analyse
576 and write
577 to textfile!</text>
578 <image>/</image>
579 <eventLine/>
580 <latch>false</latch>
581 <latched>true</latched>
582 </bsbObject>
583 </bsbPanel>
584 <bsbPresets>
585 </bsbPresets>