# Creating sound sample for sample with a loop

**URL:** <https://forum.cabbageaudio.com/t/creating-sound-sample-for-sample-with-a-loop/3268>\
**Category:** Csound Noobs\
**Created:** [4 June 2022 17:23 UTC](https://forum.cabbageaudio.com/t/creating-sound-sample-for-sample-with-a-loop/3268 "2022-06-04T17:23:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![spruitje](https://forum.cabbageaudio.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@spruitje](https://forum.cabbageaudio.com/u/spruitje)\
**Post date:** [4 June 2022 17:23 UTC](https://forum.cabbageaudio.com/t/creating-sound-sample-for-sample-with-a-loop/3268/1 "2022-06-04T17:23:21Z")

</div>

I would like to create a sound signal one sample value at a time by means of a loop. The process should run like this:

1. Ask for the start values of x\_old and v\_old.
2. Calculate x\_new and v\_new from x\_old and v\_old by means of certain recursive relations.
3. Export x\_new as the current audio sample value.
4. Rename x\_new and v\_new to x\_old and v\_old .
5. Goto 2.

The loop should take exactly one sample time long, so that one sample value is produced during one sample time.

Is there a way to do this in Csound?

---

<div class="post-metadata">

**Author:** ![spruitje](https://forum.cabbageaudio.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@spruitje](https://forum.cabbageaudio.com/u/spruitje)\
**Post date:** [5 June 2022 12:28 UTC](https://forum.cabbageaudio.com/t/creating-sound-sample-for-sample-with-a-loop/3268/2 "2022-06-05T12:28:17Z")

</div>

Looks like this kind of implicit loop will do the trick?:

```
<CsoundSynthesizer>

<CsOptions>
-odac
</CsOptions>

<CsInstruments>
sr = 44100
ksmps = 1
nchnls = 2
0dbfs = 1

instr 1
kf = 440
kres timeinsts
aOut = sin(2*$M_PI*kf*kres)
outs aOut, aOut
endin
</CsInstruments>

<CsScore>
i 1 0 6
</CsScore>

</CsoundSynthesizer>
```

---

<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:** [6 June 2022 09:23 UTC](https://forum.cabbageaudio.com/t/creating-sound-sample-for-sample-with-a-loop/3268/3 "2022-06-06T09:23:15Z")

</div>

yes, ksmps = 1 is the key 👍

---

<div class="post-metadata">

**Author:** ![spruitje](https://forum.cabbageaudio.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@spruitje](https://forum.cabbageaudio.com/u/spruitje)\
**Post date:** [6 June 2022 13:01 UTC](https://forum.cabbageaudio.com/t/creating-sound-sample-for-sample-with-a-loop/3268/4 "2022-06-06T13:01:00Z")

</div>

Thanks! 🙂

---

<div class="post-metadata">

**Author:** ![spruitje](https://forum.cabbageaudio.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@spruitje](https://forum.cabbageaudio.com/u/spruitje)\
**Post date:** [7 June 2022 20:28 UTC](https://forum.cabbageaudio.com/t/creating-sound-sample-for-sample-with-a-loop/3268/5 "2022-06-07T20:28:35Z")

</div>

```
<CsoundSynthesizer>

<CsOptions>

-o dac

</CsOptions>

<CsInstruments>

sr = 44100

ksmps = 1

nchnls = 2

0dbfs = 1

instr 1

;steps of 1 sample time

kdelta_t = 1/sr

;input R/m

ka = 7643021

;measuring time passed

kres timeinsts

if kres < 0.0001 then

kx_old = 1.0

kv_old = 0

else

kx_new = kx_old + kv_old*kdelta_t

kv_new = kv_old + -1*ka*kx_old*kdelta_t

kx_old = kx_new

kv_old = kv_new

endif

;correction of amplitude drift

kx_old limit kx_old, -1, 1

aOut = kx_old

outs aOut, aOut

endin

</CsInstruments>

<CsScore>

i 1 0 5

</CsScore>

</CsoundSynthesizer>

```

The above was what I planned to do, and it’s working now by giving a harmonic signal except for one thing: the amplitude keeps on rising if not checked by limiting the signal (as I have done here by means of the limit opcode). Why doesn’t the amplitude stay at the starting value?

---

<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:** [8 June 2022 08:20 UTC](https://forum.cabbageaudio.com/t/creating-sound-sample-for-sample-with-a-loop/3268/6 "2022-06-08T08:20:18Z")

</div>

From a quick glance it seems that you have feedback with multiplication. This line is causing the blowup:

```
kv_new = kv_old + -1*ka*kx_old*kdelta_t

```

add and printk under it and see how it grows…

```
kv_new = kv_old + -1*ka*kx_old*kdelta_t
printk .2, kv_new
```

---

<div class="post-metadata">

**Author:** ![spruitje](https://forum.cabbageaudio.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@spruitje](https://forum.cabbageaudio.com/u/spruitje)\
**Post date:** [8 June 2022 08:49 UTC](https://forum.cabbageaudio.com/t/creating-sound-sample-for-sample-with-a-loop/3268/7 "2022-06-08T08:49:49Z")

</div>

I’m trying to do something similar as this: [https://evgenii.com/blog/programming-harmonic-oscillator/](https://evgenii.com/blog/programming-harmonic-oscillator/) (see 3. and further)

But I don’t have the moving picture and I’m using Csound. There is also feedback on the web application but no blowup there…

---

<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:** [8 June 2022 09:29 UTC](https://forum.cabbageaudio.com/t/creating-sound-sample-for-sample-with-a-loop/3268/8 "2022-06-08T09:29:58Z")

</div>

Looks interesting. I don’t have time to delve into it right now, but I’m sure it’s just a simple mistake in your code. That line is clearly a problem.

---

<div class="post-metadata">

**Author:** ![spruitje](https://forum.cabbageaudio.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@spruitje](https://forum.cabbageaudio.com/u/spruitje)\
**Post date:** [9 June 2022 14:13 UTC](https://forum.cabbageaudio.com/t/creating-sound-sample-for-sample-with-a-loop/3268/9 "2022-06-09T14:13:43Z")

</div>

Problem solved with sr = 300000000 and “Render to file” which gives a wav file with a nice constant harmonic signal with hardly any distortion (no limiting needed any more):

Begin of signal:

 ![begin](https://forum.cabbageaudio.com/uploads/default/original/2X/d/d643ad93fbf9ba1799b504b001d55ff94c60f9b9.png)

End of signal:

 ![eind](https://forum.cabbageaudio.com/uploads/default/original/2X/9/95e075dc83e2b94b139d6fa53a8f4d7d58d23f27.png)
