Skip to content

Thread sync/drift #2

@whaleygeek

Description

@whaleygeek

Hi, been doing some work with CA, I noticed that if I start 3 threads running at default BPM, after a few bars they get a bit out of sync. I wasn't sure whether you had built in thread synchronisation into your core code, or whether it was up to the user to do their own sync between threads. Either are ok of course, but I wasn't sure if you were expecting users to realise that they need thread synchronisation and then read about how to do it as part of the learning experience, or whether you had provision for it to happen automatically in your code so readers could just code multiple tracks out of the box.

Code below as an example, excuse my crap ruby code I'm not really a ruby programmer: I have a separate thread showing the bar and beat number so I can keep track of everything on the screen, the thread that starts playing after 3 bars on my Raspberry Pi is already out of sync. If I remove this bar counting, it does still get out of sync eventually so I think even if there was a startup latency in the threads, there still appears to be some drift over time.

C=60
D=62
E=64
F=65
G=67
A=69
B=71
octave=12
major = [0,2,4,5,7,9,11,12]
pentatonic = [1,3,6,8,10]
d=1.0

bar=0
beat=0
in_thread do
loop do
sleep d
beat=(beat+1)%4
if beat==0
bar = bar + 1
print "**** "
print bar
print "\n"
end
end
end

by the time this thread reaches it's target bar

time has drifted enough to be noticeable

in_thread do
note=C+12
loop do
if bar>3
play note
end
sleep d/2
end
end

in_thread do
with_synth "beep"
note=C
loop do
play note+pentatonic[rand(5)]
sleep d/2
end
end

in_thread do
note=C-octave*2
with_synth "beep"
loop do
play note
sleep d
play note
sleep d
play note
sleep d/2
play note
sleep d/2
play note
sleep d
end
end

in_thread do
with_synth "saw_beep"
loop do
note = C+pentatonic[rand(5)]
play note-(octave)
sleep d
end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions