[Csnd] csound API clicks/cracks

Perhaps someone can help me troubleshoot the following C# code? I am hearing some random clicks and cracks, not just at the beginning but usually also 1-3 times while the tone is playing. I guess .NET may be not be the best language for realtime processing, but I am actually experiencing similar problems with PureBasic (which is very low-level and C-like), and on other computers.

using System;
using System.Runtime.InteropServices;

class Program
    [DllImport(@"csound64.dll")] private static extern IntPtr csoundCreate();
    [DllImport(@"csound64.dll")] private static extern long csoundSetOption(IntPtr csound, string option);
    [DllImport(@"csound64.dll")] private static extern long csoundCompileOrc(IntPtr csound, string option);
    [DllImport(@"csound64.dll")] private static extern void csoundStart(IntPtr csound);
    [DllImport(@"csound64.dll")] private static extern void csoundStop(IntPtr csound);
    [DllImport(@"csound64.dll")] private static extern long csoundPerformKsmps(IntPtr csound);
    [DllImport(@"csound64.dll")] private static extern long csoundReadScore(IntPtr csound, string option);

    static void Main(string[] args)
        IntPtr csound = csoundCreate();
        csoundSetOption(csound, "-odac0");
        csoundSetOption(csound, "-b2048");
        csoundSetOption(csound, "-B2048");
        csoundCompileOrc(csound, @"
            ga init 1
            instr 1
                ga poscil 0.5, 400
                outs ga, ga
        csoundReadScore(csound, "i 1 0 5");
        while (csoundPerformKsmps(csound) == 0) ;

Csound mailing list
Send bugs reports to
        Issues · csound/csound · GitHub
Discussions of bugs and features can be posted here

Might not be related, but why are you using a global variable in the instrument?
I presumed you tested this code in your computer just using the csound command (or another frontend) and you are
not getting the same problems.

It could be related to -b and -B. Try just using -b, you can see if it works then you can try lowering it to -b 128 or something like that. I assume portaudio can
use the better drivers, but make sure you are not using the legacy sound libraries (MME) by not selecting that output
but something else.

What you are describing sounds like dropouts which are probably caused by the buffering not doing the job correctly. The language does not really matter
in this case because you are just using it to run the engine, which is a binary library.

You can also try increasing ksmps to something higher than 10, and power of 2 is preferable.

Thanks for the help! If I remove the -B option I can not get good latency without ASIO, but it does indeed seem to help now.
I thought .NET might sometimes delay the thread so that csoundPerformKsmps would not be called in time, but I guess there is no problem with that then.
The global variable was a ‘typo’.