# Grains on fly (granular synthesis)

**URL:** <https://forum.cabbageaudio.com/t/grains-on-fly-granular-synthesis/2735>\
**Category:** Cabbage recipes\
**Created:** [28 July 2021 08:24 UTC](https://forum.cabbageaudio.com/t/grains-on-fly-granular-synthesis/2735 "2021-07-28T08:24:33Z")\
**Posts on this page:** 19\
**Page:** 2

<div class="post-metadata">

**Author:** ![rorywalsh](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/rorywalsh/32/165_2.png) [@rorywalsh](https://forum.cabbageaudio.com/u/rorywalsh)\
**Post date:** [5 September 2021 18:26 UTC](https://forum.cabbageaudio.com/t/grains-on-fly-granular-synthesis/2735/21 "2021-09-05T18:26:22Z")

</div>

I appreciate you including the English text, as it gives context to the piece. But I know I"m missing out on something by not being able to read the original. But it takes little away from my enjoyment of the piece. It makes me want to start composing again!

---

<div class="post-metadata">

**Author:** ![strapparts](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/strapparts/32/6144_2.png) [@strapparts](https://forum.cabbageaudio.com/u/strapparts)\
**Post date:** [7 September 2021 20:41 UTC](https://forum.cabbageaudio.com/t/grains-on-fly-granular-synthesis/2735/22 "2021-09-07T20:41:20Z")

</div>

Composing can be an enjoyable activity. If you have visions, realize them! But in this historical epoch there would be more need for a great computer “luthier” and I think you have the qualities and you have taken the right path.  
A collaboration between computer engineers and musician-performers and composers would be fruitful.  
Stradivari did not compose, Cristofori did not compose. Despite this they have a positive place in History.  
The universals of music have been identified in “jeu de règles, jeu sensori-moteur, jeu symbolique” (F. Delalande).  
It is possible to make interesting music with only one of these games, but the music in its maximum vitality probably remains the complete one of all “jeux”.  
Probably, Computer music should take “sensori-moteur” play into greater consideration, it should find a way to make environment, body’s energy and electrical energy work well together.  
This could perhaps be achieved with common computers, without resorting to particularly sophisticated and expensive tools.

---

<div class="post-metadata">

**Author:** ![strapparts](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/strapparts/32/6144_2.png) [@strapparts](https://forum.cabbageaudio.com/u/strapparts)\
**Post date:** [10 October 2021 09:37 UTC](https://forum.cabbageaudio.com/t/grains-on-fly-granular-synthesis/2735/23 "2021-10-10T09:37:15Z")

</div>

Hi Rory,  
I added a “gentable” to display the envelope of the recorded source. There are some drawbacks that maybe you can easily solve:

1. (the most important). When I change the value of “tab size” combobox, the system crashes. If at system startup I set value(1) or value(2), or value(3) in “combobox”, it’s ok, but if I wanted to change it on running, I would not know how to do it with “gentable”.
2. (less important) I wrote “ftfree 1, 0” to clean all the previous rec, but it’s not work.
3. (less important) Now it appears this Warning in console (I use windows 10): The following identifiers are not camelCase: “rescale”(Line:122)".  
I’m attaching the new version of the file.[GrainOnFly\_prova8.csd](https://forum.cabbageaudio.com/uploads/short-url/m1unCWLsWUyVbmL1trpcMSZZIXI.csd) (9.1 KB)

---

<div class="post-metadata">

**Author:** ![rorywalsh](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/rorywalsh/32/165_2.png) [@rorywalsh](https://forum.cabbageaudio.com/u/rorywalsh)\
**Post date:** [10 October 2021 16:39 UTC](https://forum.cabbageaudio.com/t/grains-on-fly-granular-synthesis/2735/24 "2021-10-10T16:39:43Z")

</div>

Thanks @strapparts, I’ll check this out when I get a chance and see if I can find a fix 👍

[edit] I just tested this here and I’m not getting any crash? Do you get a crash in the main Cabbage IDE, or only when running as a plugin, or both?

[edit2] sorry, I can also get it to crash, but only when I start recording. Either way, it’s something I can work with…

---

<div class="post-metadata">

**Author:** ![rorywalsh](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/rorywalsh/32/165_2.png) [@rorywalsh](https://forum.cabbageaudio.com/u/rorywalsh)\
**Post date:** [10 October 2021 18:01 UTC](https://forum.cabbageaudio.com/t/grains-on-fly-granular-synthesis/2735/25 "2021-10-10T18:01:51Z")

</div>

A possible workaround here until I fix this is the great one large table just for displaying the actual tables. So create a second table that has the maximum table size samples. Each time you record a sample, use one of the quick table copy opcodes to update table 2 with the contents of table 1, and then set the view range with [`sampleRange()`](https://cabbageaudio.com/docs/gentable/#sampleRange).

In the meantime I will try to solve the crash. I generally wouldn’t recommend changing table sizes on the fly, but in this case it should be Ok, because it will probably won’t be done that often.

---

<div class="post-metadata">

**Author:** ![rorywalsh](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/rorywalsh/32/165_2.png) [@rorywalsh](https://forum.cabbageaudio.com/u/rorywalsh)\
**Post date:** [10 October 2021 18:14 UTC](https://forum.cabbageaudio.com/t/grains-on-fly-granular-synthesis/2735/26 "2021-10-10T18:14:02Z")

</div>

I just took a deeper look at the my `gentable` code and I’m not sure we can actually resize a gentable after it has been created. Check [this](https://forum-next.cabbageaudio.com/t/updating-gentable-crashes-cabbage/2338) thread. So the option above should work, another option would be to generate the waveform on the fly using an SVG element. Hmm, this sounds like a fun challenge. I might give it a go if I get a spare moment.

---

<div class="post-metadata">

**Author:** ![strapparts](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/strapparts/32/6144_2.png) [@strapparts](https://forum.cabbageaudio.com/u/strapparts)\
**Post date:** [10 October 2021 20:53 UTC](https://forum.cabbageaudio.com/t/grains-on-fly-granular-synthesis/2735/27 "2021-10-10T20:53:58Z")

</div>

> [@rorywalsh](#):
>
> set the view range with [`sampleRange()`](https://cabbageaudio.com/docs/gentable/#sampleRange)

This would be an excellent solution, but I have the impression that sampleRange, in gentable, doesn’t work.  
I did a test by adding this instrument:  
instr 20  
kmetro metro 2  
cabbageSet kmetro, “gentable1”, “alpha”, random:k(0, 1)  
cabbageSet kmetro, “gentable1”, “sampleRange”, 0, random:k(100, 20000)  
endin  
I see “alpha” variations, but I don’t see end point variations.

---

<div class="post-metadata">

**Author:** ![rorywalsh](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/rorywalsh/32/165_2.png) [@rorywalsh](https://forum.cabbageaudio.com/u/rorywalsh)\
**Post date:** [11 October 2021 08:10 UTC](https://forum.cabbageaudio.com/t/grains-on-fly-granular-synthesis/2735/28 "2021-10-11T08:10:24Z")

</div>

Now that you mention it, I think it’s for `soundfiler`, which in this case would also work. On the other hand, it’s in the `gentable` docs, so I better take a look 🙂

---

<div class="post-metadata">

**Author:** ![rorywalsh](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/rorywalsh/32/165_2.png) [@rorywalsh](https://forum.cabbageaudio.com/u/rorywalsh)\
**Post date:** [11 October 2021 08:17 UTC](https://forum.cabbageaudio.com/t/grains-on-fly-granular-synthesis/2735/29 "2021-10-11T08:17:24Z")

</div>

Seems to work Ok when you pass the arguments to the channel as a string using `sprintfk()` as seen below. I’ll take a look at see why it fails the other way.

```
<Cabbage>
form caption("Gentable Sample Range") size(400, 300), guiMode("queue") pluginId("def1"), colour(0,0,0)
gentable bounds(0, 0, 400, 200), channel("table1"), tableNumber(1)
hrange bounds(4, 208, 392, 40) channel("min", "max") max(1) min(0) range(0, 1, 0:1, 1, 0.001)
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-n -d
</CsOptions>
<CsInstruments>
; Initialize the global variables. 
ksmps = 32
nchnls = 2
0dbfs = 1

instr 1
    iLen ftlen 1
    kMin, kTrig1 cabbageGet "min"
    kMax, kTrig2 cabbageGet "max"
    cabbageSet kTrig1+kTrig2, "table1", sprintfk("sampleRange(%d, %d)", kMin*iLen, kMax*iLen) 
endin

</CsInstruments>
<CsScore>
f1 0 1024 10 1
;causes Csound to run for about 7000 years...
f0 z
;starts instrument 1 and runs it for a week
i1 0 [60*60*24*7] 
</CsScore>
</CsoundSynthesizer>
```

---

<div class="post-metadata">

**Author:** ![strapparts](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/strapparts/32/6144_2.png) [@strapparts](https://forum.cabbageaudio.com/u/strapparts)\
**Post date:** [11 October 2021 09:51 UTC](https://forum.cabbageaudio.com/t/grains-on-fly-granular-synthesis/2735/30 "2021-10-11T09:51:04Z")

</div>

This example is fantastic!  
Ok, I learn that I have to be careful of formats (string or number) when I pass values ​​with cabbageSet. I also see that with tabsize of 32768 the example works, but with higher tabsizes (for example 131072 etc.) it doesn’t work.

---

<div class="post-metadata">

**Author:** ![strapparts](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/strapparts/32/6144_2.png) [@strapparts](https://forum.cabbageaudio.com/u/strapparts)\
**Post date:** [11 October 2021 09:55 UTC](https://forum.cabbageaudio.com/t/grains-on-fly-granular-synthesis/2735/31 "2021-10-11T09:55:57Z")

</div>

> [@rorywalsh](#):
>
> Now that you mention it, I think it’s for `soundfiler` , which in this case would also work.

as soon as I can, I will try with ‘sounfiler’

---

<div class="post-metadata">

**Author:** ![rorywalsh](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/rorywalsh/32/165_2.png) [@rorywalsh](https://forum.cabbageaudio.com/u/rorywalsh)\
**Post date:** [11 October 2021 11:25 UTC](https://forum.cabbageaudio.com/t/grains-on-fly-granular-synthesis/2735/32 "2021-10-11T11:25:02Z")

</div>

Tables with a sizes greater than 44100 can’t be updated on the fly. It’s just way too slow. That’s why the `soundfiler` widget exists. It uses much faster drawing routines. I should probably print some kind of warning to the console when this happens.

---

<div class="post-metadata">

**Author:** ![strapparts](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/strapparts/32/6144_2.png) [@strapparts](https://forum.cabbageaudio.com/u/strapparts)\
**Post date:** [11 October 2021 23:24 UTC](https://forum.cabbageaudio.com/t/grains-on-fly-granular-synthesis/2735/33 "2021-10-11T23:24:20Z")

</div>

In this thread: [https://forum-next.cabbageaudio.com/t/svgtable-example/2898/2](https://forum-next.cabbageaudio.com/t/svgtable-example/2898/2) Rory came up with an ingenious solution. Thanks, Rory! I’m attaching the new version of GrainOnFly with his solution [GrainOnFly\_prova12.csd](https://forum.cabbageaudio.com/uploads/short-url/eptpzGYptrYwwuGIyVSNeJ8sfhY.csd) (10.3 KB) .  
I also entered this line:  
cabbageSet “table1”, “svgElement”, “0”  
to reset the image when you change size tab.  
It works, don’t ask me why. I am not very familiar with svg.

---

<div class="post-metadata">

**Author:** ![rorywalsh](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/rorywalsh/32/165_2.png) [@rorywalsh](https://forum.cabbageaudio.com/u/rorywalsh)\
**Post date:** [12 October 2021 09:54 UTC](https://forum.cabbageaudio.com/t/grains-on-fly-granular-synthesis/2735/34 "2021-10-12T09:54:31Z")

</div>

> [@strapparts](#):
>
> I also entered this line:  
> cabbageSet “table1”, “svgElement”, “0”  
> to reset the image when you change size tab.  
> It works, don’t ask me why. I am not very familiar with svg.

Interesting. It works because it’s not valid SVG text so it clears the image. Nice trick 🙂

---

<div class="post-metadata">

**Author:** ![strapparts](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/strapparts/32/6144_2.png) [@strapparts](https://forum.cabbageaudio.com/u/strapparts)\
**Post date:** [12 October 2021 17:49 UTC](https://forum.cabbageaudio.com/t/grains-on-fly-granular-synthesis/2735/35 "2021-10-12T17:49:49Z")

</div>

With this request I’m cheeky, I know: could you also write valid SVG text to draw a horizontal line in the center here, instead of leaving the image completely blank? 🙂

---

<div class="post-metadata">

**Author:** ![rorywalsh](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/rorywalsh/32/165_2.png) [@rorywalsh](https://forum.cabbageaudio.com/u/rorywalsh)\
**Post date:** [12 October 2021 18:11 UTC](https://forum.cabbageaudio.com/t/grains-on-fly-granular-synthesis/2735/36 "2021-10-12T18:11:32Z")

</div>

Just add another point at the end, which cause a line to be drawn right back at the start of the:

```
 SPath strcat SPath, sprintf({{
        L%d %d L0 %d" style="fill:none;stroke:lime;stroke-width:2;stroke-linejoin:round;"/>
        }}, iBounds[2], iBounds[3]/2, iBounds[3]/2) 

```

Notice the extra L0 % in the format specifiers. You can drop this line in as a replacement for the one that is there. 👍

---

<div class="post-metadata">

**Author:** ![strapparts](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/strapparts/32/6144_2.png) [@strapparts](https://forum.cabbageaudio.com/u/strapparts)\
**Post date:** [12 October 2021 20:08 UTC](https://forum.cabbageaudio.com/t/grains-on-fly-granular-synthesis/2735/37 "2021-10-12T20:08:20Z")

</div>

Great! I was missing this horizontal line. Thank you! I attach the new csd. [GrainOnFly\_prova13.csd](https://forum.cabbageaudio.com/uploads/short-url/gu8vAVuwZknXC6kddwj7hbUU1Ap.csd) (11.3 KB) I also added the automatic recording stop when the table ends.

---

<div class="post-metadata">

**Author:** ![rorywalsh](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/rorywalsh/32/165_2.png) [@rorywalsh](https://forum.cabbageaudio.com/u/rorywalsh)\
**Post date:** [12 October 2021 20:50 UTC](https://forum.cabbageaudio.com/t/grains-on-fly-granular-synthesis/2735/38 "2021-10-12T20:50:40Z")

</div>

Thanks for posting the update 👍 I’m glad the SVG approach worked out.

---

<div class="post-metadata">

**Author:** ![strapparts](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/strapparts/32/6144_2.png) [@strapparts](https://forum.cabbageaudio.com/u/strapparts)\
**Post date:** [30 August 2026 21:44 UTC](https://forum.cabbageaudio.com/t/grains-on-fly-granular-synthesis/2735/39 "2026-08-30T21:44:02Z")

</div>

Hi to all, I am sending a version with a few improvements. I used it in [Sinfonia (2026) – strAppArts](https://www.strapparts.com/sinfonia-2026/)  
All the best. Andrea

[GrainOnFly\_2025\_effect.csd](https://forum.cabbageaudio.com/uploads/short-url/9c1i3f7UDr4CBnVVYaAMXYgJ2r3.csd) (15.7 KB)

[Previous page](https://forum.cabbageaudio.com/t/grains-on-fly-granular-synthesis/2735.md?page=1)
