# Note control delayed with synth \`dsaw\`

**URL:** <https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293>\
**Category:** Support, Help & Resources\
**Created:** [March 22, 2021, 9:50pm UTC](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293 "2021-03-22T21:50:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlange](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/mlange/32/1899_2.png) [@mlange](https://in-thread.sonic-pi.net/u/mlange)\
**Post date:** [March 22, 2021, 9:50pm UTC](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293/1 "2021-03-22T21:50:09Z")

</div>

Hi together!

While working on a track, in the more calm sections I had the impression that something is slightliy off in terms of timing/synchronization.

After some investigation, I found out that this is probably caused by a small delay when controlling the note played by the `dsaw` synth I use there (using `control syn, note: ...`).

Using Audacity, I could visualize it with the spectrogram view (screenshot below). At the top is the `dsaw`, with the cursor at the beat position. The change of the synth note is to the right of it, so somewhat delayed (0.03 - 0.04 sec). In the middle is `saw`, which shows a similar delay, but to a lesser extent. At the bottom is `tb303`, which does not show the delay, or at least it is too short to be visible.

 ![grafik](https://europe1.discourse-cdn.com/flex017/uploads/sonic_pi/original/2X/c/c29abb3620709d475d249b31ab0e335b68bf0b17.jpeg)

Here is a minimal code example that reproduces the effect. It was also used for recording for the above image.

```ruby
live_loop :tick, delay: 0.01 do
  8.times do
    use_sample_defaults finish: 0.05
    sample :elec_twip
    sleep 0.5
  end
end

live_loop :bass, sync: :tick do
  with_synth :dsaw do
    use_synth_defaults detune: 0, sustain: 8, release: 0, amp: 0.4
    syn = play :e2
    4.times do
      control syn, note: (scale :f2, :minor_pentatonic).choose
      sleep 2
    end
  end
end

```

Does anybody know whether this is expected behaviour for these synths, or if it might be a bug? I am quite sure it is not a performance problem (scsynth is below 1% CPU load).

Many thanks!

---

<div class="post-metadata">

**Author:** ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)\
**Post date:** [March 23, 2021, 9:59am UTC](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293/2 "2021-03-23T09:59:19Z")

</div>

Hi there,

this is expected behaviour. Both the initial call to `play` and the subsequent call to `control` are converted to separate messages which are sent to the synthesis engine. At this stage, it’s possible for the messages to be received out of order. In order to guarantee that the order is preserved, in the case where both the initial trigger and and a following control are set to the same time stamp, (i.e. no call to `sleep` happens between them) then the control is delayed just slightly to ensure it arrives afterwards.

To avoid this, always ensure that there’s a sleep between the `play` and `control`.

In the future it may be possible to detect that this is happening prior to sending the messages out and then to automatically merge any similarly timed `control` into the `play` of a specific synth node as if you were just adding additional options to the `play`. However, this would require a considerable amount of “smarts” to be added to that layer of the system and isn’t particularly high priority at this stage.

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [March 23, 2021, 10:07am UTC](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293/3 "2021-03-23T10:07:12Z")

</div>

Following on from @samaaron 's answer you could try this:

```ruby
live_loop :tick, delay: 0.01 do
  8.times do
    use_sample_defaults finish: 0.05
    sample :elec_twip
    sleep 0.5
  end
end

live_loop :bass, sync: :tick do
  with_synth :dsaw do
    use_synth_defaults detune: 0, sustain: 8, release: 0, amp: 0.4
    syn = play (scale :f2, :minor_pentatonic).choose
    sleep 2
    3.times do
      control syn, note: (scale :f2, :minor_pentatonic).choose
      sleep 2
    end
  end
end

```

---

<div class="post-metadata">

**Author:** ![mlange](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/mlange/32/1899_2.png) [@mlange](https://in-thread.sonic-pi.net/u/mlange)\
**Post date:** [March 23, 2021, 10:16am UTC](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293/4 "2021-03-23T10:16:39Z")

</div>

Hi Sam and Robin,

thanks a lot for your comprehensive answers! I will try the suggested solution ASAP.

Do I understand correctly that subsequent calls to `control` are delayed the same as the first one?

Is there a logic reason why the effect is not visible with `tb303`?

Thanks again!

---

<div class="post-metadata">

**Author:** ![mlange](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/mlange/32/1899_2.png) [@mlange](https://in-thread.sonic-pi.net/u/mlange)\
**Post date:** [March 23, 2021, 11:55am UTC](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293/5 "2021-03-23T11:55:02Z")

</div>

I tried the code suggested by @robin.newman, but the delay did not change significantly:

 ![grafik](https://europe1.discourse-cdn.com/flex017/uploads/sonic_pi/original/2X/0/0fdc24fda417761a12cfeb19991e67d5eb5f87cc.png)

In my “real” project, it is the same. Currently, I shorten the first note by 0.06 beats / 0.03 sec to compensate, but it is not so cool to have to measure the delay for every synth to be used with note control.

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [March 23, 2021, 4:17pm UTC](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293/6 "2021-03-23T16:17:37Z")

</div>

I wonder if the detune affects the attack that you hear. Do you notice any difference if you set detune to 0, or to 12?

---

<div class="post-metadata">

**Author:** ![mlange](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/mlange/32/1899_2.png) [@mlange](https://in-thread.sonic-pi.net/u/mlange)\
**Post date:** [March 23, 2021, 5:40pm UTC](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293/7 "2021-03-23T17:40:47Z")

</div>

No, not really. It is on 12 in the original project, and to change it to 0 was the first thing I tried. The example for the images also uses 0.

I will try the recommendation of not immediatly using `control`, with the `saw` synth, to see if it changes anything there.

---

<div class="post-metadata">

**Author:** ![mlange](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/mlange/32/1899_2.png) [@mlange](https://in-thread.sonic-pi.net/u/mlange)\
**Post date:** [March 23, 2021, 7:54pm UTC](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293/8 "2021-03-23T19:54:18Z")

</div>

I can confirm that the problem also persists for the `saw` synth, although using `sleep 2` between `play` and `control`

 ![grafik](https://europe1.discourse-cdn.com/flex017/uploads/sonic_pi/original/2X/0/097497e6a9d9faac23b0d5a0db7e335f3b315f79.png)

---

<div class="post-metadata">

**Author:** ![mlange](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/mlange/32/1899_2.png) [@mlange](https://in-thread.sonic-pi.net/u/mlange)\
**Post date:** [March 23, 2021, 8:02pm UTC](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293/9 "2021-03-23T20:02:03Z")

</div>

I had the idea that the issue might be caused by the relatively low frequencies used in the tests, like maybe a new note only starts when an already running wave/period finishes. But using high frequencies like `:f5` does not change anything either.

EDIT: Further, I tried to control different opts, still with sleeping between `play` and `control`. It appears that `cutoff` (top) changes with a delay, while `amp` (bottom) does not (or at least less delay than the length of the tick sound, here 0.03):

 ![grafik](https://europe1.discourse-cdn.com/flex017/uploads/sonic_pi/original/2X/8/8f2a5faf1bee26e22bcf2d50890ba3fed6fa0b5b.jpeg)

---

<div class="post-metadata">

**Author:** ![mlange](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/mlange/32/1899_2.png) [@mlange](https://in-thread.sonic-pi.net/u/mlange)\
**Post date:** [March 25, 2021, 10:50pm UTC](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293/10 "2021-03-25T22:50:57Z")

</div>

Should I report this as a potential bug on GitHub, so it does not slip away in case devs consider it as important?

---

<div class="post-metadata">

**Author:** ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)\
**Post date:** [March 25, 2021, 11:36pm UTC](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293/11 "2021-03-25T23:36:46Z")

</div>

Hi, could you try changing the following in the source code of your app to `0` and see if that improves things for you:

> <https://github.com/sonic-pi-net/sonic-pi/blob/00f737e67adb571018f853c0af911cb684255e42/app/server/ruby/lib/sonicpi/util.rb#L256>

---

<div class="post-metadata">

**Author:** ![mlange](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/mlange/32/1899_2.png) [@mlange](https://in-thread.sonic-pi.net/u/mlange)\
**Post date:** [March 26, 2021, 9:42am UTC](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293/12 "2021-03-26T09:42:45Z")

</div>

No, it’s still the same (tested for controlling the note played by `dsaw`, again).

Also, if the `default_control_delta` is in seconds or beats, the observed delta is an order of magnitude larger, with around 0.03 seconds.

---

<div class="post-metadata">

**Author:** ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)\
**Post date:** [March 26, 2021, 5:09pm UTC](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293/13 "2021-03-26T17:09:40Z")

</div>

In that case, it’s quite likely to be related to the audio buffer size. Supercollider can’t issue a synth control change in the middle of computing an audio buffer. It can only do it at the buffer boundaries. Therefore the larger the buffer size, the larger the potential error for each control message. Note that the error is always there and bounded by the buffer size. Happy to explain this more if it doesn’t make sense.

You can play with the audio buffer size if you edit the new audio settings toml file in the config dir. this is located within the .sonic-pi dir within your home dir.

Let me know if changing the buffer size changes the results you see 🙂

---

<div class="post-metadata">

**Author:** ![mlange](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/mlange/32/1899_2.png) [@mlange](https://in-thread.sonic-pi.net/u/mlange)\
**Post date:** [March 26, 2021, 5:49pm UTC](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293/14 "2021-03-26T17:49:16Z")

</div>

Thanks for this further suggestion! So you refer to `sound_card_buffer_size` here?

When I go below 512, I get no sound at all. With 512, the delay seems unchanged, but I get artifacts in the sound. With 1024 and above, sound is ok, but the delay if still there. Not being able to go lower is probably a limitation of my sound card…

Also, I am wondering why the `tb303` does not show any visible delay, as it should also be affected by the buffer boundary “error”.

In the project where the issue first appeared for me, I could circumvent its audible effect by shortenting the first note played by the respective synth (published to in\_thread [here](https://in-thread.sonic-pi.net/t/first-sonic-pi-track-made-with-literate-programming/5308/7)). But still, I would be interested in ways to preventing the issue from ground.

---

<div class="post-metadata">

**Author:** ![Nechoj](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nechoj/32/1814_2.png) [@Nechoj](https://in-thread.sonic-pi.net/u/Nechoj)\
**Post date:** [March 26, 2021, 8:19pm UTC](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293/15 "2021-03-26T20:19:51Z")

</div>

Just a proposal. Eliminating the `choose` operation just before the `control` excludes a potential influence on the timing:

```ruby
live_loop :tick, delay: 0.01 do
  8.times do
    use_sample_defaults finish: 0.05
    sample :elec_twip
    sleep 0.5
  end
end

live_loop :bass, sync: :tick do
  with_synth :dsaw do
    use_synth_defaults detune: 0, sustain: 8, release: 0, amp: 0.4
    syn = play (scale :f2, :minor_pentatonic).choose
    notes = (scale :f2, :minor_pentatonic).pick(3)
    sleep 2
    notes.each do |n|
      control syn, note: n
      sleep 2
    end
  end
end

```

I haven’t done measurement, but when listening to it I somehow got the impression it is more on the point now. But it is hard to hear a difference at all.

---

<div class="post-metadata">

**Author:** ![mlange](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/mlange/32/1899_2.png) [@mlange](https://in-thread.sonic-pi.net/u/mlange)\
**Post date:** [March 26, 2021, 8:47pm UTC](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293/16 "2021-03-26T20:47:11Z")

</div>

Thanks, @Nechoj!

However, the code shown here is only a minimal reproducing example. It uses `choose` only incidentially. The original project plays notes from arrays that are generated before any live loop starts.

For details on that project, see [this forum thread](https://in-thread.sonic-pi.net/t/first-sonic-pi-track-made-with-literate-programming/5308/6) (which you already commented on 😉). Code and documentation are linked there. The recorded track there is already corrected through a shortened first note.

EDIT: it is the [bass live loop](https://mlange-42.github.io/Sonic-LP/first-steps/house-bounce.html#bass) that is affected there. `bass_delay` is the correction.

---

<div class="post-metadata">

**Author:** ![mlange](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/mlange/32/1899_2.png) [@mlange](https://in-thread.sonic-pi.net/u/mlange)\
**Post date:** [March 26, 2021, 8:50pm UTC](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293/17 "2021-03-26T20:50:16Z")

</div>

> I haven’t done measurement, but when listening to it I somehow got the impression it is more on the point now. But it is hard to hear a difference at all.

In the example presented here, I also don’t hear the delay, but visualized it (see screenshots). In the original project, however, it was audible in the uncorrected version.

---

<div class="post-metadata">

**Author:** ![Nechoj](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nechoj/32/1814_2.png) [@Nechoj](https://in-thread.sonic-pi.net/u/Nechoj)\
**Post date:** [March 26, 2021, 8:59pm UTC](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293/18 "2021-03-26T20:59:47Z")

</div>

Now I did some measurement as well. For that purpose I just changed `control`line to

```ruby
control syn, note: n, amp: [0.2, 0.6].tick

```

because then the Ableton recording shows the amplitude envelope. In most cases I see this:

![test1](https://europe1.discourse-cdn.com/flex017/uploads/sonic_pi/original/2X/7/7a0ddfa64a918cd7bb786f1387e3048055a93424.jpeg)

or this

![test2](https://europe1.discourse-cdn.com/flex017/uploads/sonic_pi/original/2X/7/7cf4def3f9a5d38f3dda7e9cd1b1733c28f3a544.jpeg)

And in about 1 out of 10 it looks like that

![test3](https://europe1.discourse-cdn.com/flex017/uploads/sonic_pi/original/2X/5/56871045f6652285242d1cacd073a510ee8919c1.jpeg)

It looks as if there is some task switching in the OS going on which affects the timing of SPI. Is the `control` command executed within a new thread?

---

<div class="post-metadata">

**Author:** ![mlange](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/mlange/32/1899_2.png) [@mlange](https://in-thread.sonic-pi.net/u/mlange)\
**Post date:** [March 26, 2021, 9:14pm UTC](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293/19 "2021-03-26T21:14:44Z")

</div>

Thanks for this test! For me, the delay in the note change is consistent in every call to `control`. As already reported [here](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293/9), the delay seems to affect only some of the opts, which does not include `amp`. However, I used the spectroscope visualization (in Audacity) as I found it better suited to see the relatively short delay.

> Is the `control` command executed within a new thread?

Yes, playing the melody in the original is “released” to a separate thread using `at do`, in order to control other opts of the synth and an FX while playing.

---

<div class="post-metadata">

**Author:** ![Nechoj](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nechoj/32/1814_2.png) [@Nechoj](https://in-thread.sonic-pi.net/u/Nechoj)\
**Post date:** [March 26, 2021, 9:21pm UTC](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293/20 "2021-03-26T21:21:47Z")

</div>

Ok, if its consistent in every call on your machine and only in the note opt, then my observation apparently is a different issue and can be caused by the separate thread. The delay in the above picture is 1/32 = 0.03125 s.

[Next page](https://in-thread.sonic-pi.net/t/note-control-delayed-with-synth-dsaw/5293.md?page=2)
