Hi all,
I’ve spent the last three days trying to debug an audio glitch in a Csound instrument I’ve made, but I’ve gotten nowhere. The instrument is a looper with a crossfade that uses the mincer
opcode. The code is included below. Note that the k-variables defined at the top in the braces are default values defined for csoundengine.
Here’s an overview of how the looper works:
- The first
while
loop moveskphase_main
across the function table where the wav sample is stored. - When
kphase_main
crosses thekboundary_right
it resets to the starting point of the sample and then activates the crossfade that starts wherekphase_main
left off. - The second
while
loop then moveskphase_fade
through the crossfade tail while a decreasing amplitude envelope is triggered forkcrossfade_duration
. - When
kcrossfade_duration
has passed the crossfade tail deactivates.
Here’s the code:
instr looper
{kplayback_position = 0, kamp = 0.5, initial_phase = 0, kboundary_left = 0, kboundary_right = 0, kfunction_table = 1, kphase_reset = 0, kloop_mode = 1, kcrossfade_duration = 0.3}
ispeed = 1
kamp_global_env expsegr 0.005, 0.01, 1, 0.01, 0.001
kphase_main init initial_phase
aphase_main init 0
kfade_active init 0
aphase_fade init 0
kcount_fade init 0
kamp_fade_env init 1
kcount_test init 1
ktrig changed2 kphase_reset
if (ktrig == 1) then
kphase_main = kboundary_left
endif
if (kphase_main > kboundary_right) then
if kloop_mode == 0 then
turnoff
elseif kloop_mode == 1 then
kfade_active = 1
kphase_fade = kphase_main
kphase_main = kboundary_left
endif
endif
kindex = 0
while (kindex < ksmps) do
aphase_main[kindex] = kphase_main / sr
kphase_main = kphase_main + 1
kindex += 1
od
kamp_main_total = a(kamp_global_env) * kamp
asig_main mincer aphase_main, kamp_main_total, ispeed, kfunction_table, 1
if kfade_active == 1 then
kfade_frames = round((kcrossfade_duration * sr) / ksmps)
if (kcount_fade < kfade_frames) then
kindex = 0
while (kindex < ksmps) do
aphase_fade[kindex] = kphase_fade / sr
kphase_fade = kphase_fade + 1
kindex += 1
od
kamp_fade_env = (kfade_frames - kcount_fade) / kfade_frames
kcount_fade += 1
kamp_fade_total = kamp_global_env * kamp_fade_env * kamp
asig_fade mincer aphase_fade, kamp_fade_total, ispeed, kfunction_table, 1
else
kfade_active = 0
kcount_fade = 0
asig_fade = 0
aphase_fade = 0
endif
endif
chnmix asig_main + asig_fade, "mix"
endin
The crossfade works beautifully for the first play through the loop. Starting with the second play through the loop the crossfade starts with a split-second audio glitch that sounds as though the kphase_fade
is briefly reading from a different part of the function table. I’ve isolated this audio glitch to just the crossfade mincer
, not the main mincer
. I’ve done a bunch of print debugging to ensure that the k-variables (e.g. phase, amplitude envelope) have the values they are supposed to, and everything looks correct. I don’t see evidence that the kphase_fade
is briefly reading from a different part of the table.
I can’t figure out why the first activation of the crossfade sounds perfect and then future activations begin with a split second audio glitch. Are any of you able to see where the bug in the code might be?
Thanks!
Jason