# Linseg not working with grain?

**URL:** <https://forum.csound.com/t/linseg-not-working-with-grain/1070>\
**Category:** Newbies\
**Created:** [February 2, 2023, 2:15pm UTC](https://forum.csound.com/t/linseg-not-working-with-grain/1070 "2023-02-02T14:15:37Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dionysis](https://forum.csound.com/user_avatar/forum.csound.com/dionysis/32/325_2.png) [@Dionysis](https://forum.csound.com/u/Dionysis)\
**Post date:** [February 2, 2023, 2:15pm UTC](https://forum.csound.com/t/linseg-not-working-with-grain/1070/1 "2023-02-02T14:15:37Z")

</div>

Hello to all!

This is the second time I am studying Csound.

First time it was around 2005, when I created some pieces that I still like to this day. Actually they were just the exercises of chapter 1, which I tried to make as interesting as possible, and some of them ended up so nice to my ears that I considered them finished works. I was using qbasic back then to feed random parameters to the score file of Csound. Of course Csound can do that itself, but I hadn’t yet read the relevant chapter. For some reason I stopped working after a while but I always wanted to come back to Csound and I’m glad I finally did! If anyone wants to hear my old pieces I give a link at the end of the message. This is not as much self-promotion as wanting to share my joy with you! (The Csound pieces are the “studies”, “pieces” and the “miniature”, possibly “meditation” as well).

Moving on to the present: I started again from scratch, doing my homework. The canon exercise on chapter 1 seemed promising. I didn’t get that right and made a canon for all 6 instruments instead of just for instrument 106. So much for the better! Things got more interesting when the exercise called for changing amplitudes and frequencies for each instrument. I got something in my hands that can evolve to a keeper. But there are still things I don’t know how to do. One of them is avoid clicking sounds when a sound stops abruptly. I don’t mind them in my older pieces, but in this one they stand out. A friend, who is an experienced Csound user, taught me about the linseg opcode. Nevertheless, grain is not affected by it and still makes an annoying clicking sound at the end of it. I know all this will be explained sooner than later in the book, but somehow this got me stuck. And although there are many more things to improve on it (stereo panning would be nice), I’d like to ask if there is a simple way to remedy the grain problem now.

Thank you all!  
Dionysis

> **[Electronic Music](https://boukouvalas.weebly.com/electronic-music.html)**
>
> Meditation (12/2005) \[7'\] Due to the silent character of the piece and the presence of high harmonics, it should be listened to at a quiet environement. No outward sounds should be heard while...

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [February 3, 2023, 11:57am UTC](https://forum.csound.com/t/linseg-not-working-with-grain/1070/2 "2023-02-03T11:57:18Z")

</div>

Hi @Dionysis I have no idea what book you are even referring to here? Can you be a little more specific/ Also, if you can prepare a very simple example of code that illustrates the issue? Often through the process of reducing your code you’ll find your answer, and if not, someone here might be able to help.

---

<div class="post-metadata">

**Author:** ![Dionysis](https://forum.csound.com/user_avatar/forum.csound.com/dionysis/32/325_2.png) [@Dionysis](https://forum.csound.com/u/Dionysis)\
**Post date:** [February 3, 2023, 12:54pm UTC](https://forum.csound.com/t/linseg-not-working-with-grain/1070/3 "2023-02-03T12:54:17Z")

</div>

Ah yes! I’m referring to The Csound Book (edited by Richard Boulanger).  
I don’t think the problem is example-specific, but this is how the instrument looks like:

```csound

      instr 105
k1 linseg 0, p3*0.01, 1, p3*0.98, 1, p3*0.01, 0
a1 grain 50, 440, 55, 5000, 10, .05, 1, 3, 1 ;asynchronous granular synthesis
     out a1
	endin

```

As I said, linseg has no effect to it whatsoever. Even if I modify the linseg to something like this:  
k1 linseg 0, p3_0.1, 1, p3_0.7, 1, p3\*0.2, 0

there is no change whatsoever.  
Grain does not seem to be affected by it.

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [February 3, 2023, 1:57pm UTC](https://forum.csound.com/t/linseg-not-working-with-grain/1070/4 "2023-02-03T13:57:26Z")

</div>

No it won’t have any effect because you’re not using the output of the `linseg` opcode. Do you mean to use it to control the other amplitude of the instrument? If so you can do:

```csound
out a1*k1

```

Or you can use k1 to control the amplitude of each successive grain. 👍

---

<div class="post-metadata">

**Author:** ![Dionysis](https://forum.csound.com/user_avatar/forum.csound.com/dionysis/32/325_2.png) [@Dionysis](https://forum.csound.com/u/Dionysis)\
**Post date:** [February 3, 2023, 2:24pm UTC](https://forum.csound.com/t/linseg-not-working-with-grain/1070/5 "2023-02-03T14:24:15Z")

</div>

> [@rory](#):
>
> `out a1*k1`

Great! That solved it! Thank you!

---

<div class="post-metadata">

**Author:** ![Dionysis](https://forum.csound.com/user_avatar/forum.csound.com/dionysis/32/325_2.png) [@Dionysis](https://forum.csound.com/u/Dionysis)\
**Post date:** [February 3, 2023, 3:01pm UTC](https://forum.csound.com/t/linseg-not-working-with-grain/1070/6 "2023-02-03T15:01:13Z")

</div>

While we’re at it:  
The grain amplitude parameter does not seem to have any effect.  
I’m referring to the first parameter after the grain opcode (signifying the “amplitude of each grain”, accodring to the reference manual). This is 50 in the example above. Even if I make that a 5 the overal volume produced by grain does not change.

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [February 3, 2023, 3:20pm UTC](https://forum.csound.com/t/linseg-not-working-with-grain/1070/7 "2023-02-03T15:20:45Z")

</div>

Do you have 0dbfs set in your orchestra?

---

<div class="post-metadata">

**Author:** ![Dionysis](https://forum.csound.com/user_avatar/forum.csound.com/dionysis/32/325_2.png) [@Dionysis](https://forum.csound.com/u/Dionysis)\
**Post date:** [February 3, 2023, 5:31pm UTC](https://forum.csound.com/t/linseg-not-working-with-grain/1070/8 "2023-02-03T17:31:37Z")

</div>

Very interesting! Didn’t know about that.  
I read about it, but it is still a bit beyond my grasp.  
Only thing to ask here: Why is grain not influenced by a change of the amplitude parameter? Reference manual says the parameter defines the aplitude of each grain. Shouldn’t a change in the parts (i.e. grains) result in a change in the sum?  
Thank you!

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [February 4, 2023, 7:53am UTC](https://forum.csound.com/t/linseg-not-working-with-grain/1070/9 "2023-02-04T07:53:12Z")

</div>

It certainly should. Can you post your full csd?

---

<div class="post-metadata">

**Author:** ![Dionysis](https://forum.csound.com/user_avatar/forum.csound.com/dionysis/32/325_2.png) [@Dionysis](https://forum.csound.com/u/Dionysis)\
**Post date:** [February 4, 2023, 5:05pm UTC](https://forum.csound.com/t/linseg-not-working-with-grain/1070/10 "2023-02-04T17:05:55Z")

</div>

When I copy/paste the code it is not displayed in the same way. Do I have to change some forum setting or something?

---

<div class="post-metadata">

**Author:** ![Dionysis](https://forum.csound.com/user_avatar/forum.csound.com/dionysis/32/325_2.png) [@Dionysis](https://forum.csound.com/u/Dionysis)\
**Post date:** [February 4, 2023, 5:08pm UTC](https://forum.csound.com/t/linseg-not-working-with-grain/1070/11 "2023-02-04T17:08:49Z")

</div>

And it says that as a new user I cannot upload attachments (I trying uploading the csd file instead).

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [February 4, 2023, 5:41pm UTC](https://forum.csound.com/t/linseg-not-working-with-grain/1070/12 "2023-02-04T17:41:29Z")

</div>

Just paste the code, then select it all and press the code format button in the editor window, i.e, ‘\</\>’ 👍

---

<div class="post-metadata">

**Author:** ![Dionysis](https://forum.csound.com/user_avatar/forum.csound.com/dionysis/32/325_2.png) [@Dionysis](https://forum.csound.com/u/Dionysis)\
**Post date:** [February 4, 2023, 5:57pm UTC](https://forum.csound.com/t/linseg-not-working-with-grain/1070/13 "2023-02-04T17:57:55Z")

</div>

Oh, great! Here it is 🙂

```csound
<CsoundSynthesizer>
<CsOptions>
</CsOptions>
<CsInstruments>
sr = 44100
kr = 4410
ksmps = 10
nchnls = 1

		instr 101
k1 linseg 0, p3*0.001, 1, p3*0.989, 1, p3*0.01, 0
a1 oscil 1000*k1, 420, 1
	out a1
		endin

		instr 102
k1 linseg 0, p3*0.001, 1, p3*0.989, 1, p3*0.01, 0
a1 foscil 300*k1, 425, 1, 2, 3, 1 ;simple 2 operator fm opcode
	out a1
		endin

		instr 103
k1 linseg 0, p3*0.001, 1, p3*0.989, 1, p3*0.01, 0
a1 buzz 1500*k1, 430, 10, 1 ;variable pulse train
	out a1
		endin

		instr 104					
k1 linseg 0, p3*0.001, 1, p3*0.989, 1, p3*0.01, 0
a1 pluck 1500*k1, 435, 440, 2, 1 ;karplus-strong plucked string
	out a1
		endin

		instr 105
k1 linseg 0, p3*0.01, 1, p3*0.98, 1, p3*0.01, 0
a1 grain 5, 440, 55, 5000, 10, .05, 1, 3, 1 ;asynchronous granular synthesis
	out a1*k1
		endin
		
		instr 106					
k1 linseg 0, p3*0.001, 1, p3*0.989, 1, p3*0.01, 0
a1 loscil 2000*k1, 445, 4 ;sample-based looping oscillator
	out a1
		endin
		

</CsInstruments>
<CsScore>
; Function 1 uses the GEN10 subroutine to compute a sine wave
; Function 2 uses the GEN10 subroutine to compute the first sixteen partials of a sawtooth wave
; Function 3 uses the GEN20 subroutine to compute a Hanning window for use as a grain envelope
; Function 4 uses the GEN01 subroutine to read in an AIF audio file

f 1 0 4096 10 1    
f 2 0 4096 10 1 .5 .333 .25 .2 .166 .142 .125 .111 .1 .09 .083 .076 .071 .066 .062
f 3 0 4097 20 2 1
f 4 0 0 1 "sing.aif" 0 4 0

;inst	start	duration
i 101 90.9632 .6696435 
i 101 94.9632 .6696435 
i 102 25.60291 1.096932 
i 102 29.60291 1.096932 
i 101 47.30704 2.450471 
i 101 51.30704 2.450471 
i 104 64.08577 1.344757 
i 104 68.08577 1.344757 
i 101 43.6591 1.792804 
i 101 47.6591 1.792804 
i 101 13.04319 1.770715 
i 101 17.04319 1.770715 
i 102 79.40641 .8661251 
i 102 83.40641 .8661251 
i 101 52.47598 1.181398 
i 101 56.47598 1.181398 
i 104 94.02682 1.639377 
i 104 98.02682 1.639377 
i 104 99.23355 .8433657 
i 104 103.2336 .8433657 
i 101 21.91711 1.700547 
i 101 25.91711 1.700547 
i 101 118.9671 2.16165 
i 101 122.9671 2.16165 
i 104 119.4712 .6056205 
i 104 123.4712 .6056205 
i 106 105.3327 .5045258 
i 106 109.3327 .5045258 
i 103 112.408 2.156835 
i 103 116.408 2.156835 
i 105 89.44437 1.826625 
i 105 93.44437 1.826625 
i 101 67.35001 1.58735 
i 101 71.35001 1.58735 
i 104 58.57738 1.813155 
i 104 62.57738 1.813155 
i 101 83.65102 1.036526 
i 101 87.65102 1.036526 
i 105 26.12118 1.298697 
i 105 30.12118 1.298697 
i 101 15.47443 .6065043 
i 101 19.47443 .6065043 
i 105 10.78261 1.935453 
i 105 14.78261 1.935453 
i 101 71.12131 .6229498 
i 101 75.12131 .6229498 
i 105 46.03904 1.907142 
i 105 50.03904 1.907142 
i 101 111.6086 3.912411 
i 101 115.6086 3.912411 
i 102 98.84144 4.953221 
i 102 102.8414 4.953221 
i 101 68.49706 1.412328 
i 101 72.49706 1.412328 
i 104 18.56757 1.65545 
i 104 22.56757 1.65545 
i 103 28.96971 .9245311 
i 103 32.96971 .9245311 
i 101 .2250483 1.780807 
i 101 4.225048 1.780807 
i 101 109.1596 1.293507 
i 101 113.1596 1.293507 
i 106 109.9045 .7060223 
i 106 113.9045 .7060223 
i 101 115.7283 .5053823 
i 101 119.7283 .5053823 
i 102 82.48276 .6937002 
i 102 86.48276 .6937002 
i 103 84.6935 .9567131 
i 103 88.6935 .9567131 
i 104 117.5758 1.169552 
i 104 121.5758 1.169552 
i 105 3.507779 3.600261 
i 105 7.507779 3.600261 
i 104 11.74201 2.596025 
i 104 15.74201 2.596025 
i 103 47.38626 1.812237 
i 103 51.38626 1.812237 
i 101 14.93535 .8347535 
i 101 18.93535 .8347535 
i 104 119.8849 .757066 
i 104 123.8849 .757066 
i 101 23.20836 1.763843 
i 101 27.20836 1.763843 
i 101 28.72862 1.38114 
i 101 32.72862 1.38114 
i 101 11.66535 .6166117 
i 101 15.66535 .6166117 
i 102 54.88942 1.819416 
i 102 58.88942 1.819416 
i 104 13.66552 .8807217 
i 104 17.66552 .8807217 
i 105 99.91445 .6610541 
i 105 103.9145 .6610541 
i 104 9.276052 1.142622 
i 104 13.27605 1.142622 
i 104 73.50423 1.163532 
i 104 77.50423 1.163532 
i 103 85.97529 .900946 
i 103 89.97529 .900946 
i 104 69.34071 .6824014 
i 104 73.34071 .6824014 
i 106 24.60582 1.53841 
i 106 28.60582 1.53841 
i 106 87.1654 1.740891 
i 106 91.1654 1.740891 
i 103 4.577617 1.655365 
i 103 8.577617 1.655365 
i 104 27.1075 .7680649 
i 104 31.1075 .7680649 
i 102 8.321161 .7238661 
i 102 12.32116 .7238661 
i 104 77.26206 .7985026 
i 104 81.26206 .7985026 
i 104 102.9906 .7304335 
i 104 106.9906 .7304335 
i 101 42.51805 1.370514 
i 101 46.51805 1.370514 
i 101 26.41671 .9267135 
i 101 30.41671 .9267135 
i 101 34.63651 1.099923 
i 101 38.63651 1.099923 
i 101 13.31033 .868055 
i 101 17.31033 .868055 
i 106 103.5787 .5472243 
i 106 107.5787 .5472243 
i 105 78.71521 1.705514 
i 105 82.71521 1.705514 
i 104 20.08415 .7859056 
i 104 24.08415 .7859056 
i 101 57.71123 1.641441 
i 101 61.71123 1.641441 
i 101 56.49891 2.062245 
i 101 60.49891 2.062245 
i 105 38.36755 .6292029 
i 105 42.36755 .6292029 
i 102 103.8535 .5975636 
i 102 107.8535 .5975636 
i 103 98.9457 .8463961 
i 103 102.9457 .8463961 
i 104 29.1913 1.688609 
i 104 33.1913 1.688609 
i 101 75.35273 .899085 
i 101 79.35273 .899085 
i 101 63.1463 .8656267 
i 101 67.1463 .8656267 
i 102 84.84431 .9841571 
i 102 88.84431 .9841571 
i 101 7.771581 .795005 
i 101 11.77158 .795005 
i 101 111.9779 2.799601 
i 101 115.9779 2.799601 
i 104 47.61725 1.52142 
i 104 51.61725 1.52142 
i 101 108.8157 .5930825 
i 101 112.8157 .5930825 
i 104 18.05861 1.650855 
i 104 22.05861 1.650855 
i 105 4.378244 .5397065 
i 105 8.378244 .5397065 
i 101 34.87428 .7421463 
i 101 38.87428 .7421463 
i 101 107.3468 .7311081 
i 101 111.3468 .7311081 
i 104 65.07434 3.298205 
i 104 69.07434 3.298205 
i 101 87.01721 1.711004 
i 101 91.01721 1.711004 
i 104 11.97843 1.62659 
i 104 15.97843 1.62659 
i 104 41.5025 .991407 
i 104 45.5025 .991407 
i 101 72.54389 .9546885 
i 101 76.54389 .9546885 
i 101 23.18629 2.777502 
i 101 27.18629 2.777502 
i 103 35.94387 1.409314 
i 103 39.94387 1.409314 
i 101 78.42583 1.667812 
i 101 82.42583 1.667812 
i 104 64.39548 .7320039 
i 104 68.39548 .7320039 
i 106 75.5051 1.656293 
i 106 79.5051 1.656293 
i 103 52.23788 .5144066 
i 103 56.23788 .5144066 
i 105 46.838 .783142 
i 105 50.838 .783142 
i 102 104.2604 .7965052 
i 102 108.2604 .7965052 
i 101 77.4214 2.218903 
i 101 81.4214 2.218903 
i 102 76.28086 1.029204 
i 102 80.28086 1.029204 
i 101 102.5383 1.820341 
i 101 106.5383 1.820341 
i 101 113.9733 1.458827 
i 101 117.9733 1.458827 
i 101 98.71107 .7516712 
i 101 102.7111 .7516712 

</CsScore>
</CsoundSynthesizer>

```

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [February 6, 2023, 11:25am UTC](https://forum.csound.com/t/linseg-not-working-with-grain/1070/14 "2023-02-06T11:25:42Z")

</div>

Currently, your `0dbfs` value is circa 32000, because you haven’t specified different. I’d use `0dbfs=1` so that your max gain across all instruments is 1. It is easier to work out the correct amplitudes when this is the case. Right now you’ve set your grain amplitude to be 5/32000, which really isn’t much at all. And changing it to 1000 isn’t going to make much of a difference either.

p.s be careful changing `0dbfs`, all of your other opcodes will need to have their amp values brought below 1 also, otherwise you risk blowing your ears off! So please turn down your speakers on the first run after making these changes 🙂

---

<div class="post-metadata">

**Author:** ![Dionysis](https://forum.csound.com/user_avatar/forum.csound.com/dionysis/32/325_2.png) [@Dionysis](https://forum.csound.com/u/Dionysis)\
**Post date:** [February 6, 2023, 12:16pm UTC](https://forum.csound.com/t/linseg-not-working-with-grain/1070/15 "2023-02-06T12:16:39Z")

</div>

ok I’ll try that, thank you!  
All instruments will refer to the same scale afterwards? (i.e. 0-1)  
I guess I’ll have to keep it e.g. up to 0.5, because many sounds at the same spot would have their amplitudes added, risking distortion, right?

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [February 6, 2023, 1:20pm UTC](https://forum.csound.com/t/linseg-not-working-with-grain/1070/16 "2023-02-06T13:20:05Z")

</div>

> [@Dionysis](#):
>
> I guess I’ll have to keep it e.g. up to 0.5, because many sounds at the same spot would have their amplitudes added, risking distortion, right?

This is correct 🙂 All outputs summed must be below 1.

---

<div class="post-metadata">

**Author:** ![Dionysis](https://forum.csound.com/user_avatar/forum.csound.com/dionysis/32/325_2.png) [@Dionysis](https://forum.csound.com/u/Dionysis)\
**Post date:** [February 6, 2023, 2:11pm UTC](https://forum.csound.com/t/linseg-not-working-with-grain/1070/17 "2023-02-06T14:11:27Z")

</div>

So, when 0dbfs is not defined (as in my initial program) there is no risk of clipping?

Of course that doesn’t answer why the amplitude change in grain had no effect in the first place…

---

<div class="post-metadata">

**Author:** ![joachim](https://forum.csound.com/letter_avatar_proxy/v4/letter/j/a87d85/32.png) [@joachim](https://forum.csound.com/u/joachim)\
**Post date:** [February 6, 2023, 4:18pm UTC](https://forum.csound.com/t/linseg-not-working-with-grain/1070/18 "2023-02-06T16:18:30Z")

</div>

the risk of clipping is always there, whatever 0dbfs you set.

if you do not set as rory suggested …

0dbfs = 1

… then you use the default value which is because of historical  
reasons 32767 (15 bit).

when you use the suggested 0-1 range, all what is larger than 1 is clipping.  
when you use the default value, all what is larger than 32767 is clipping.

best -  
joachim

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [February 6, 2023, 7:32pm UTC](https://forum.csound.com/t/linseg-not-working-with-grain/1070/19 "2023-02-06T19:32:55Z")

</div>

> [@Dionysis](#):
>
> Of course that doesn’t answer why the amplitude change in grain had no effect in the first place…

I think it does.

> [@Dionysis](#):
>
> This is 50 in the example above. Even if I make that a 5 the overal volume produced by grain does not change.

You’re changing the amplitude from 0.001 to 0.0001, I’m not surprised you don’t hear a difference. Of course, technically there is a difference, but I can’t imagine it would be easy to hear. Try reworking your code to make full range of +/-32767 or set 0dbfs to 1 and use a full range of +/-1
